【问题标题】:How to find which annotation send showDetails?如何找到哪个注释发送 showDetails?
【发布时间】:2010-12-30 18:30:06
【问题描述】:

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

【问题讨论】:

    标签: iphone mkpinannotationview


    【解决方案1】:

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

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

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

    【讨论】:

    • 但是他想知道在众多注释中哪一个被点击了!
    • @carbonr, view.annotation 是被点击的那个。我建议使用委托方法而不是 addTarget。
    • @carbonr,如果您必须或想要使用 addTarget 而不是委托,请参阅此答案:stackoverflow.com/a/9876255/467105
    • 这非常有用。谢谢! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多