在calloutAccessoryControl上检测仅在rightCalloutAccessoryView上点击 [英] Detect on calloutAccessoryControlTapped only the tap on rightCalloutAccessoryView

查看:101
本文介绍了在calloutAccessoryControl上检测仅在rightCalloutAccessoryView上点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击注释视图和

My calloutAccessoryControlTapped is also called when I just tap on annotation view and this behavior it's right. But how can I detect if the user has tapped on the right accessory view (in my case a detail disclosure button) and not just in the view?

我添加了一个简单的检查,但是它不起作用.

I added a simple check but it doesn't work.

import UIKit
import MapKit

extension MapVC: MKMapViewDelegate, CLLocationManagerDelegate
{    
    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl)
    {
        if control == view.rightCalloutAccessoryView
        {
            ... // enter here even if I tapped on the view annotation and not on button
        }
    }

}

推荐答案

要实现此目的,您需要为正确的附件视图添加目标.您可以通过将按钮设置为 rightCalloutAccessoryView 来实现,如代码片段所示.

To achieve it you would need to add target for the right accessory view. You can achieve it by setting button to rightCalloutAccessoryView as shown in the code snippet.

class MapViewController: UIViewController, MKMapViewDelegate {

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is Annotation {
            let annotationView = AnnotationView(annotation: annotation, reuseIdentifier: "reuseIdentifier")
            let rightButton = UIButton(type: .DetailDisclosure)
            rightButton.addTarget(self, action: #selector(didClickDetailDisclosure(_:)), forControlEvents: .TouchUpInside)
            annotationView.rightCalloutAccessoryView = rightButton
        }
        return nil
    }

    func didClickDetailDisclosure(button: UIButton) {
        // TODO: Perform action when was clicked on right callout accessory view.
    }
}

// Helper classes.
class Annotation: NSObject, MKAnnotation {
    var coordinate: CLLocationCoordinate2D
    var title: String?
    var subtitle: String?

    init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String) {
        self.coordinate = coordinate
        self.title = title
        self.subtitle = subtitle
    }
}

class AnnotationView: MKAnnotationView {

}

这篇关于在calloutAccessoryControl上检测仅在rightCalloutAccessoryView上点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆