我正在尝试使用非常简单的自定义地图注释视图和标注 - 创建注释视图时,只需将 UIImageView 作为子视图添加到其自身。效果很好。
但是,当我在注释视图上调用 canShowCallout 时,返回视图后立即在 MapKit 中引发异常。堆栈的末尾看起来像:
#0 0x94e964e6 in objc_exception_throw
#1 0x01e26404 in -[MKOverlayView _addViewForAnnotation:]
#2 0x01e22037 in -[MKOverlayView _addViewsForAnnotations:animated:]
#3 0x01e1ddf9 in -[MKOverlayView showAddedAnnotationsAnimated:]
#4 0x01df9c0e in -[MKMapView _showAddedAnnotationsAndRouteAnimated:]
#5 0x01e0371a in -[MKMapView levelView:didLoadTile:]
我的 viewForAnnotation 非常简单:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ( ! [annotation isKindOfClass:[MyAnnotation class]] )
return nil;
MyAnnotationView *useView = (MyAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:@"resuseview"];
if ( useView == nil )
{
useView = [[[MyAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"resuseview"] autorelease];
useView.canShowCallout = YES; // if commented out view appears just fine
}
else
{ useView.annotation = annotation; }
return useView;
}
正如代码中所述,注释视图按原样工作正常 - 直到我添加 canShowCallout,然后它在地图第一次获取视图时崩溃。
答案是 MyAnnotation(实现了 MKAnnotation 协议)没有实现两个可选的协议方法:
- (NSString *)subtitle;
- (NSString *)title;
因为我计划了一个完全自定义的标注,所以我认为我不需要定义这些 - 并且调用堆栈没有显示无法识别的选择器。
此外,我实现这两个只是为了返回 nil,但发现为了让注释真正激活标注,title
方法(至少)必须返回一个非零值,否则将不会显示标注。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)