2011-03-03 5 views
0

열거 형에 액세스하려고 시도하지만 작동하지 않습니다. 열거 형 - 예기되지 않은 선택기가 인스턴스에 전송되었습니다.

typedef enum 
{ 
    AnnotationTypeMale = 0, 
    AnnotationTypeFemale = 1 
} AnnotationType; 

@interface Annotation : NSObject <MKAnnotation> 
{ 

    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle; 
    AnnotationType annotation_type; 
} 



@property (nonatomic) CLLocationCoordinate2D coordinate; 
@property (nonatomic,retain) NSString *title; 
@property (nonatomic,retain) NSString *subtitle; 
@property (nonatomic,getter=getAnnotationType,setter=setAnnotationType) AnnotationType  annotation_type; 

@end 

이 내 Annotation.h이었고, 내 Annotation.mi에 ...

내가 내 Annotation.h의 형식 정의 열거하고 내가 열거의 하나의 요소에 액세스하기 위해 다른 클래스에서 시도 합성 모든 그리고 난 내가 AnnotationType에 액세스하려면 지금하려고 내 다른 클래스 ... 또한 Annotation.h ... 을 포함

문 나던 오류가 발생 work..this 경우 생성
- (AnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation 
{ 
AnnotationView *annotationView = nil; 


// determine the type of annotation, and produce the correct type of annotation view for it. 
Annotation* myAnnotation = (Annotation *)annotation; 



if([myAnnotation getAnnotationType] == AnnotationTypeMale) 
{ 

: 인해 캐치되지 않는 예외 응용 프로그램 종료 ' NSInvalidArgumentException ', reason :'- [MKUserLocation getAnnotationTyp e] : 인스턴스에 보낸 인식 할 수없는 선택기 0x5c43850 '

모든 솔루션 ?????? thx

답변

6

오류 메시지는 [MKUserLocation getAnnotationType]: unrecognized selector...입니다. 이는 viewForAnnotation 메소드가 MKUserLocation 유형의 주석에 대해 getAnnotationType을 호출하려고한다는 것을 의미합니다.

지도보기에서 showsUserLocation을 YES로 설정해야합니다. 즉, 추가중인 유형 Annotation의 주석 외에도 사용자 위치에 대한 자체 파란색 점 주석 (유형이 MKUserLocation)이 추가됩니다.

viewForAnnotation에서 Annotation처럼 치료하려고 시도하기 전에 어떤 유형의 주석이 있는지 확인해야합니다. 확인하지 않으므로 코드는 유형에 관계없이 모든 유형의 주석에 대해 getAnnotationType을 호출하려고 시도하지만 MKUserLocation에는 예외가 발생하므로 이러한 메소드가 없습니다.

당신은 주석 유형의 MKUserLocation 인 경우 확인하고 즉시 전무를 반환 할 수 있습니다 :

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{ 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 

    //your existing code... 
} 

또는 주석 형 주석의 경우 확인하고 특정 코드를 실행합니다

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{ 
    MKAnnotationView *annotationView = nil; 

    if ([annotation isKindOfClass:[Annotation class]]) 
    { 
     // determine the type of annotation, and produce the correct type of annotation view for it. 
     Annotation* myAnnotation = (Annotation *)annotation; 

     if([myAnnotation getAnnotationType] == AnnotationTypeMale) 
     { 
      //do something... 
     } 
     else 
      //do something else… 
    } 

    return annotationView; 
} 
+0

안녕을! !! !! 고마워요 4 귀하의 게시물에 문제가 고정되어 :) – Kun19

+0

첫 번째 솔루션을했다;) – Kun19