Swift 注释点击按钮

Swift Annotation click button

我有一个带有自定义注释的 mapView。我还为此添加了一个按钮,但有没有办法查看按下按钮的注释?

代码如下:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if !(annotation is CustomPointAnnotation) {
        return nil
    }

    let reuseId = "test"

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView!.canShowCallout = true
        anView!.enabled = true


        anView?.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) 

        let imageview_left = UIImageView(frame: CGRectMake(0, 0, 20, 20))
        imageview_left.image = UIImage(named: "left.png")
        anView?.leftCalloutAccessoryView = imageview_left

        let imageview_right = UIImageView(frame: CGRectMake(0, 0, 8, 13))
        imageview_right.image = UIImage(named: "right.png")
        anView!.rightCalloutAccessoryView = imageview_right

    }
    else {
        anView!.annotation = annotation
    }


    let subtitleView = UILabel()
    subtitleView.font = UIFont(name: "GillSans-Light", size: 14)
    subtitleView.numberOfLines = 1
    subtitleView.text = annotation.subtitle!
    anView!.detailCalloutAccessoryView = subtitleView


    let cpa = annotation as! CustomPointAnnotation
    anView!.image = UIImage(named:cpa.imageName)

    return anView
}

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {

    if control == view.rightCalloutAccessoryView {
        print("Pressed!")

    }
    print("Click")
}

当我点击右侧的CalloutAccessoryView 时没有任何反应。当我点击标题或副标题注释时也没有任何反应。我做错了什么?

根据苹果文档,仅当它们是 UIControl

的后代时,才会调用用于按下 leftCalloutAccessoryViewrightCalloutAccessoryView 的委托方法

因此,要么这样做,要么通过将 UITapGestureRecognizer 的实例添加到适当的视图来自己处理手势。 请记住,如果这些是 UIImageView 的实例,您还必须设置 instance.userInteractionEnabled = true

docs

你这样做的方法是向按钮添加一个目标:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    // ...

    if anView == nil {
        // ... 
        let button = MyButton(type: .DetailDisclosure)
        button.addTarget(self, action: #selector(ViewController.showAnnotationDisclosure(_:)), forControlEvents: .TouchUpInside)

        anView!.rightCalloutAccessoryView = button
    }

    return anView
}

func showAnnotationDisclosure(sender: AnyObject) {
    print("Disclosure button clicked")
}

现在如果你想知道点击了什么注释,你必须继承 UIButton:

class MyButton : UIButton {
    var annotation: CustomPointAnnotation? = nil
}

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    // ...
    if anView == nil {
        // ...
        let button = MyButton(type: .DetailDisclosure)
        button.addTarget(self, action: #selector(ViewController.showAnnotationDisclosure(_:)), forControlEvents: .TouchUpInside)

        anView!.rightCalloutAccessoryView = button
    }

    if let button = anView!.rightCalloutAccessoryView as? MyButton {
        button.annotation = annotation
    }

    return anView
}

func showAnnotationDisclosure(sender: MyButton) {
    print("Disclosure button clicked")
    print(sender.annotation?.title)
}