如何识别哪个引脚被点击 [英] How to recognize which pin was tapped

查看:18
本文介绍了如何识别哪个引脚被点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 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;

我怎么知道,哪个引脚被窃听了?谢谢

How should i know, which pin was tapped? Thnx

推荐答案

showDetails: 方法中,您可以从地图视图的 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);


顺便说一下,您可以使用地图视图的 calloutAccessoryControlTapped 委托方法,而不是执行 addTarget 并实现自定义方法.view 参数中提供了被点击的注释:


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);
}

如果您使用 calloutAccessoryControlTapped,请确保从 viewForAnnotation 中删除 addTarget.

Make sure you remove the addTarget from viewForAnnotation if you use calloutAccessoryControlTapped.

这篇关于如何识别哪个引脚被点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆