2012-02-27 78 views
0

我有一个应用程序MKMapView和大量的针脚在这张地图上。如何识别哪个销被挖掘

每个销钉都得到了rightCalloutAccessoryView。我用这种方式创建它:

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
[rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside]; 
pinView.rightCalloutAccessoryView = rightButton; 

我应该怎么知道,哪个针被点击? Thnx

回答

10

showDetails:方法中,您可以从地图视图的selectedAnnotations数组中获取引脚。虽然属性是一个NSArray,刚刚得到的数组中的第一个项目,因为地图视图只允许一次选中一个针:与其做addTarget和实施

//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委托方法。窃听注释在view参数可用:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
    calloutAccessoryControlTapped:(UIControl *)control 
{ 
    NSLog(@"ann.title = %@", view.annotation.title); 
} 

务必从viewForAnnotation删除addTarget如果使用calloutAccessoryControlTapped

+1

如果2个注释的标题相同,该怎么办? – 2015-09-23 09:40:37

+0

我知道我们可以继承和做,有没有更简单的方法? – 2015-09-23 09:47:33

+0

@安娜,这真的帮助我..谢谢+1 – 2016-01-16 17:21:49