在注释视图上使用canShowCallout时的MKMapKit异常

Ken*_*ner 5 iphone annotations mkmapview

我正在尝试使用非常简单的自定义地图注释视图和标注 - 我创建它时的注释视图,只是将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:]
Run Code Online (Sandbox Code Playgroud)

我的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;
}
Run Code Online (Sandbox Code Playgroud)

如代码中所述,注释视图工作正常 - 直到我添加canShowCallout,然后它在地图第一次获取视图时崩溃.

Ken*_*ner 13

答案结果是MyAnnotation(实现MKAnnotation协议)没有实现两个可选的协议方法:

- (NSString *)subtitle;
- (NSString *)title;
Run Code Online (Sandbox Code Playgroud)

因为我已经计划完全自定义标注,我认为我不需要定义这些 - 并且调用堆栈没有显示无法识别的选择器.

另外,我实现了这两个只是为了返回nil,但是发现为了使注释实际上激活一个callout,该title方法(至少)必须返回一个非nil值,否则将不会显示callout.