我试图访问我的枚举,但它不起作用!
我在Annotation.h中创建了一个typedef枚举,我尝试在另一个类中访问枚举的一个元素...
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
Run Code Online (Sandbox Code Playgroud)
这是我的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)
{
Run Code Online (Sandbox Code Playgroud)
if语句不起作用..发生此错误:由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [MKUserLocation getAnnotationType]:无法识别的选择器发送到实例0x5c43850'
任何解决方案?????? 谢谢
小智 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...
}
Run Code Online (Sandbox Code Playgroud)
或检查注释是否为Annotation类型并执行该特定代码:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2408 次 |
| 最近记录: |