问题描述
我的应用程序带有 MKMapView
以及此地图上的大量针脚。
I got an app with MKMapView
and large number of pins on this map.
每个针脚都有 rightCalloutAccessoryView
。我以这种方式创建它:
Every pin got rightCalloutAccessoryView
. I create it in this way:
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
我怎么知道,哪个引脚被点击了? Thnx
How should i know, which pin was tapped? Thnx
推荐答案
在 showDetails:
方法中,你可以获得pin从地图视图的 selectedAnnotations
数组中点击。即使该属性是 NSArray
,也只是获取数组中的第一项,因为地图视图一次只允许选择一个引脚:
In the showDetails:
method, you can get the pin tapped from the map view's selectedAnnotations
array. Even though the property is an NSArray
, just get the first item in the array since the map view only allows one pin to be selected at a time:
//To be safe, may want to check that array has at least one item first.
id<MKAnnotation> ann = [[mapView selectedAnnotations] objectAtIndex:0];
// OR if you have custom annotation class with other properties...
// (in this case may also want to check class of object first)
YourAnnotationClass *ann = [[mapView selectedAnnotations] objectAtIndex:0];
NSLog(@"ann.title = %@", ann.title);
顺便说一句,而不是做 addTarget
并实现自定义方法,您可以使用地图视图的 calloutAccessoryControlTapped
委托方法。点击的注释可在视图
参数中找到:
By the way, instead of doing addTarget
and implementing a custom method, you can use the map view's calloutAccessoryControlTapped
delegate method. The annotation tapped is available in the view
parameter:
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(@"ann.title = %@", view.annotation.title);
}
确保删除 addTarget
来自 viewForAnnotation
如果您使用 calloutAccessoryControlTapped
。
Make sure you remove the addTarget
from viewForAnnotation
if you use calloutAccessoryControlTapped
.
这篇关于如何识别敲击了哪个引脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!