如何识别敲击了哪个引脚

时间:2012-02-27 09:30:27

标签: ios xcode mapkit mkannotation mkannotationview

我在这张地图上找到了一个MKMapView和大量针脚的应用。

每个引脚都有rightCalloutAccessoryView。我以这种方式创建它:

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

我怎么知道,哪个引脚被敲了?日Thnx

1 个答案:

答案 0 :(得分:10)

showDetails:方法中,您可以从地图视图的selectedAnnotations数组中获取引脚。即使属性是NSArray,也只需获取数组中的第一项,因为地图视图一次只允许选择一个引脚:

//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并实现自定义方法。点按的注释可在view参数中找到:

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

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

相关问题