在注释视图上使用 canShowCallout 时出现 MKMapKit 异常 [英] MKMapKit exception when using canShowCallout on annotation view

查看:17
本文介绍了在注释视图上使用 canShowCallout 时出现 MKMapKit 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用非常简单的自定义地图注释视图和标注 - 创建注释视图时,只需添加一个 UIImageView 作为其自身的子视图.效果很好.

I'm trying to use a pretty straightforward custom map annotation view and callout - the annotation view when I create it, just adds a UIImageView as a subview to itself. That works fine.

但是,当我在注释视图上调用 canShowCallout 时,在返回视图后立即在 MapKit 中抛出异常.堆栈的末尾看起来像:

However, when I call canShowCallout on the annotation view, An exception is thrown in MapKit immediately after returning the view. The end of the stack looks like:

#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 非常简单:

My viewForAnnotation is pretty simple:

- (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,然后它在地图第一次获取视图时崩溃.

As noted in the code, the annotation view works fine as is - until I add canShowCallout, then it crashes the first time the map gets the view.

推荐答案

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

The answer turned out to be that MyAnnotation (which implements the MKAnnotation protocol) did not implement the two optional protocol methods:

- (NSString *)subtitle;
- (NSString *)title;

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

Because I had planned on a totally custom callout, I did not think I needed to define these - and the call stack did not show unrecognized selectors.

此外,我实现这两个只是为了返回 nil,但发现为了让注解真正激活标注,title 方法(至少)必须返回一个非 nil 值,或者否则不会显示标注.

Additionally, I implemented these two just to return nil, but found that in order for an annotation to actually activate a callout the title method (at least) MUST return a non-nil value, or else the callout will not be presented.

这篇关于在注释视图上使用 canShowCallout 时出现 MKMapKit 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆