MKMapKit exception when using canShowCallout on annotation view
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.
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:]
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 ( use开发者_JAVA百科View == 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;
}
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.
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.
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.
精彩评论