Right Callout Accessory方法及实现
我想知道是否有人可以告诉我如何向地图注释添加正确的标注附件。我尝试的一切似乎都没有取得任何成果,因此任何帮助将不胜感激。
编辑
我已经尝试过这行代码,但注释没有发生任何不同的情况。
- (MKAnnotationView *)mapview:(MKMapView *)sender viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *aView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@""];
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
aView.canShowCallout = YES;
aView.annotation = annotation;
return aView;
}
I was wondering if anyone could tell me how to add a right callout accessory to a map annotation. everything i try doesn't seem to be getting anywhere, so any help would be appreciated.
EDIT
I have tried this line of code but nothing different happens to the annotation.
- (MKAnnotationView *)mapview:(MKMapView *)sender viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *aView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@""];
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
aView.canShowCallout = YES;
aView.annotation = annotation;
return aView;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
方法名称错误。它应该是
mapView
并带有大写的V
:Objective-C 区分大小写。
如果该方法仍然没有被调用,那么另一个问题是地图视图的
delegate
未设置。在代码中,将其设置为self
或在 Interface Builder 中将委托附加到文件的所有者。另外,请确保在添加注释之前设置注释的
title
,否则标注仍然不会显示。上述更改应该可以修复附件按钮不出现的问题。
其他一些不相关的建议...
在
viewForAnnotation
中,您应该通过调用dequeueReusableAnnotationViewWithIdentifier
来支持注释视图重用:如果您的项目使用ARC,请删除
autorelease
代码>.顺便说一句,要响应附件按钮按下,请实现 calloutAccessoryControlTapped 委托方法:
The method name is wrong. It should be
mapView
with a capitalV
:Objective-C is case-sensitive.
If the method still doesn't get called, then the other problem is that the map view's
delegate
is not set. In code, set it toself
or in Interface Builder attach the delegate to File's Owner.Also make sure you set the
title
of the annotation before adding it otherwise the callout still won't show.The above changes should fix the accessory button not appearing.
Some other unrelated suggestions...
In
viewForAnnotation
, you should support annotation view re-use by callingdequeueReusableAnnotationViewWithIdentifier
:If your project uses ARC, remove the
autorelease
.By the way, to respond to the accessory button press, implement the
calloutAccessoryControlTapped
delegate method: