我编写了这段代码来创建自定义注释图像
- (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
, 你必须设置 canShowCallout
至YES
(您分配/初始化的新 View 的默认值为 NO
)。
如果您不覆盖该委托(delegate)方法,则 map View 会创建一个带有 canShowCallout
的默认红色图钉。已设置为 YES
.
然而,即使使用 canShowCallout
设置为 YES
,如果注解的 title
仍然不会出现标注是 nil
或空白(空字符串) .
(但同样,如果 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;
关于objective-c - iOS 5 自定义注释未显示其标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11312110/