2013-11-26 7 views
4

단추로 사용자 정의 설명 선에 대한보기를 만들었습니다. 이 사용자 정의 콜 아웃은 Mapview의 모든 주석을 클릭하면 표시됩니다. 하지만 사용자 정의 콜 아웃 내부의 버튼을 클릭해도 아무런 변화가 없습니다.UIView 내부에서 IBAction이 호출되지 않습니다.

#import <UIKit/UIKit.h> 

@interface TestView : UIView 


@end 

#import "TestView.h" 

@implementation TestView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

-(IBAction)showMessage:(id)sender 
{ 
    NSLog(@"Test"); 
} 

/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
} 
*/ 

@end 

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { 
    if(![view.annotation isKindOfClass:[MKUserLocation class]]) { 

     CGRect rect = CGRectMake(0,0,268,140); 

     TestView *testview = [[TestView alloc] initWithFrame:rect]; 

     testview = [self loadNibNamed:@"TestView" ofClass:[TestView class]]; 


     [view addSubview:testview]; 
    } 
} 

뷰는 loadNibNamed 메소드를 사용하여로드됩니다.

- (id)loadNibNamed:(NSString *)nibName ofClass:(Class)objClass { 
    if (nibName && objClass) { 
     NSArray *objects = [[NSBundle mainBundle] loadNibNamed:nibName owner:nil options:nil]; 

     for (id currentObject in objects){ 
      if ([currentObject isKindOfClass:objClass]) 
       return currentObject; 
     } 
    } 

    return nil; 
} 

enter image description here

나는보기와 버튼의 userInteractionEnabled 속성을 확인하고 모두 true로 설정되어 있습니다.

버튼의 내부 위로 터치 이벤트로 showMessage를 매핑했습니다.

이 문제를 식별하는 방법에 대한 제안 사항은 무엇입니까?

+0

는 일반적인 오류는 단순히 IB 내에서 제대로 동작을 연결하지 않습니다. 그걸 확인해 봤어? – trojanfoe

+0

감사합니다. 같은 스크린 샷이 포함되어 있습니다. – rshankar

+0

방금 ​​실수를 발견했습니다. [view addSubview : testview]; [mapView addSubview : testview]로 대체해야합니다. 도와 주셔서 감사합니다. – rshankar

답변

3

개체의 인스턴스를 얻기 위해, 다음 그리고 TestView

+ (TestView *)getNewTestView { 
    TestView *view = nil; 
    NSArray *xib = [[NSBundle mainBundle] loadNibNamed:@"TestView" owner:nil options:nil]; 
    for (NSObject *obj in xib) { 
     if ([obj isKindOfClass:[TestView class]]) { 
      view = (TestView *)obj; 
     } 
    } 
    return view; 

}

을 얻을 그리고 다음 정적 방법을 사용해보십시오, 사용 :

TestView *testView = [TestView getNewTestView]; 
[self.view addSubview:testView]; 
+0

self.view에 추가하면 실제로 문제가 해결되고 올바른 방법으로 보입니다. 그래서 나는 이것을 해결책으로 받아 들였다. 고맙습니다 – rshankar

0

당신의 MKAnnotationView 확인하세요 테스트 뷰를 채울 충분한 공간이 있습니다.
testView보다 작 으면 바깥 쪽 공간이 어떤 액션에도 응답하지 않습니다.
clipsToBounds를 NO로 설정하면 실제 공간을 체크 아웃 할 수 있습니다.

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { 
if(![view.annotation isKindOfClass:[MKUserLocation class]]) { 
    view.clipsToBounds = NO; 
    CGRect rect = CGRectMake(0,0,268,140); 

    TestView *testview = [[TestView alloc] initWithFrame:rect]; 

    testview = [self loadNibNamed:@"TestView" ofClass:[TestView class]]; 


    [view addSubview:testview]; 
} 

}

관련 문제