如何找到哪个注释发送showDetails?

时间:2010-12-30 18:30:07

标签: iphone mkpinannotationview

如何找到哪个注释发送showDetails?

MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
                                             initWithAnnotation:annotation reuseIdentifier:BridgeAnnotationIdentifier] autorelease];
            customPinView.pinColor = MKPinAnnotationColorPurple;
            customPinView.animatesDrop = YES;
            customPinView.canShowCallout = YES;

            // add a detail disclosure button to the callout which will open a new view controller page
            //
            // note: you can assign a specific call out accessory view, or as MKMapViewDelegate you can implement:
            //  - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
            //
            UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [rightButton addTarget:self
                            action:@selector(showDetails:)
                  forControlEvents:UIControlEventTouchUpInside];
            customPinView.rightCalloutAccessoryView = rightButton;

            return customPinView;

- (void)showDetails:(id)sender
{
  some code
}

1 个答案:

答案 0 :(得分:8)

代码中的注释有答案。不使用自定义方法并调用addTarget,而是使用map视图的calloutAccessoryControlTapped委托方法。在此方法中,您将获得对注释视图的引用,该注释视图包含对注释的引用。

删除对addTarget的调用,并将“showDetails”方法替换为:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
    calloutAccessoryControlTapped:(UIControl *)control
{
    MyAnnotationClass *annot = (MyAnnotationClass *)view.annotation;
    //do something...
}