在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
。
如果2个注释的标题相同,该怎么办? – 2015-09-23 09:40:37
我知道我们可以继承和做,有没有更简单的方法? – 2015-09-23 09:47:33
@安娜,这真的帮助我..谢谢+1 – 2016-01-16 17:21:49