iOS 5自定义注释未显示其标题

Cam*_*mus 1 annotations objective-c ios

我编写了这段代码来创建自定义注释图像

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    static NSString *google = @"googlePin";
    if ([annotation isKindOfClass:[myClass class]])
    {
        MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:google];
        if (!annotationView)
        {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:google];
            annotationView.image = [UIImage imageNamed:@"pin.png"];
        }
        return annotationView;
    }
    return nil;

}
Run Code Online (Sandbox Code Playgroud)

图像出现在地图上; 但是当我点击它时没有任何事情发生,没有标题或副标题.

你们有什么想法吗?

小智 12

覆盖时viewForAnnotation,必须设置canShowCalloutYES(您在alloc/init的新视图上的默认值NO).

如果不覆盖该委托方法,则地图视图会创建一个canShowCallout已设置为的默认红色图钉YES.

但是,即使canShowCallout设置为YES,如果注释titlenil空或空白(空字符串),则仍会显示标注.

(但同样,如果title不是nil而且不是空白,除非canShowCallout是,否则标注不会显示YES.)

MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:google];
if (!annotationView)
{
    annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:google];
    annotationView.image = [UIImage imageNamed:@"pin.png"];
    annotationView.canShowCallout = YES;  // <-- add this
}
else
{
    // unrelated but should handle view re-use...
    annotationView.annotation = annotation; 
}

return annotationView;
Run Code Online (Sandbox Code Playgroud)