代码之家  ›  专栏  ›  技术社区  ›  Kendall Helmstetter Gelner

在注释视图上使用canShowCallout时出现mkmapkit异常

  •  5
  • Kendall Helmstetter Gelner  · 技术社区  · 14 年前

    我正在尝试使用一个非常简单的自定义地图注释视图和标注-创建注释视图时,只需将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:]
    

    我对注释的看法很简单:

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

    1 回复  |  直到 11 年前
        1
  •  13
  •   Kendall Helmstetter Gelner    11 年前

    答案是,myannotation(实现mkannotation协议)没有实现两个可选的协议方法:

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

    因为我计划使用一个完全自定义的调用,所以我认为我不需要定义这些调用堆栈,而且调用堆栈没有显示无法识别的选择器。

    另外,我实现了这两个函数只是为了返回nil,但是发现为了让注释实际激活一个callout, title 方法(至少)必须返回非零值,否则将不显示标注。