Swift Annotation单击按钮

时间:2016-08-28 09:02:20

标签: ios swift mkmapview mkannotation mkannotationview

我有一个带有自定义注释的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")
}

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

2 个答案:

答案 0 :(得分:1)

您这样做的方法是向按钮添加目标:

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)
}

答案 1 :(得分:0)

根据苹果文档,只有在leftCalloutAccessoryView

的后代才会调用代表rightCalloutAccessoryViewUIControl的代表方法。

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

docs