2011-03-03 56 views
0

我嘗試訪問我的枚舉,但它不工作!枚舉 - 無法識別的選擇發送到實例

我做一個typedef枚舉我Annotation.h和我嘗試在其他類來訪問枚舉的一個元素......

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 ...

- (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) 
{ 

如果語句犯規work..this發生錯誤:終止應用程序由於未捕獲的異常' NSInvalidArgumentException',原因:' - [MKUserLocation getAnnotationTyp E]:無法識別的選擇發送到實例0x5c43850'

任何解決方案?????? THX

回答

6

錯誤說[MKUserLocation getAnnotationType]: unrecognized selector...。這意味着viewForAnnotation方法試圖在MKUserLocation類型的註釋上調用getAnnotationType。

在你的地圖視圖,showsUserLocation必須設置爲YES,這意味着地圖加入了自己的藍色圓點標註爲用戶的位置(類型MKUserLocation的),除了類型的Annotation要添加的註釋。

在viewForAnnotation,你需要檢查它試圖把它當作你的Annotation之前什麼類型的註解。由於您沒有檢查,代碼嘗試在每種類型的註釋上調用getAnnotationType,而不管類型如何,但MKUserLocation沒有這種方法,因此您會得到異常。

您可以檢查註釋類型MKUserLocation,並立即返回nil:

- (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 2011-03-06 16:33:08

+0

我採取了第一個解決方案;) – Kun19 2011-03-06 16:33:34

相關問題