1

如何找到哪个注释发送 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
}
4

1 回答 1

8

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

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

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
    calloutAccessoryControlTapped:(UIControl *)control
{
    MyAnnotationClass *annot = (MyAnnotationClass *)view.annotation;
    //do something...
}
于 2010-12-30T18:50:23.490 回答