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

标签 objective-c ios annotations

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

 - (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;

}

图像出现在 map 上;但是,当我单击它时,什么也没有发生,没有标题也没有副标题。

你们有什么想法吗?

最佳答案

当您覆盖 viewForAnnotation , 你必须设置 canShowCalloutYES (您分配/初始化的新 View 的默认值为 NO )。

如果您不覆盖该委托(delegate)方法,则 map View 会创建一个带有 canShowCallout 的默认红色图钉。已设置为 YES .

然而,即使使用 canShowCallout设置为 YES ,如果注解的 title 仍然不会出现标注是 nil或空白(空字符串) .

(但同样,如果 title 不是 nil 并且不是空白,则标注不会显示,除非 canShowCalloutYES。)

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;

关于objective-c - iOS 5 自定义注释未显示其标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11312110/

相关文章:

objective-c - 如何使用 NSTimer?

ios - class_copyPropertyList 不适用于 RLMObject

ios - UIView 扩展获取 UITableViewCell?

ios - 谷歌地图 - 3D View 是否可能

java - 使用 Spring @ManagedNotification 注释生成 JMX 通知的任何示例

Spring @Autowired 混淆(容器或 session )

iOS:UIView 层次结构:使 Map View 在 UIView 下处于事件状态

ios - 无法导入桥接头 '/MyProject-Bridging.h'

python - Python 类型提示(注释)是否会导致一些运行时影响?

ios - 使用 AFNetworking 进行 x-www-form-urlencoded POST 时出现问题