我有这样创建的自定义MKPinAnnotationView:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if annotation is MKUserLocation {
        //return nil so map view draws "blue dot" for standard user location
        return nil
    }

    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView

    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = false
        pinView!.animatesDrop = true
        pinView!.pinColor = .Red
        pinView!.draggable = true;
    } else {
        pinView!.annotation = annotation
    }

    return pinView
}

然后,我稍后使用如下按钮向其添加子视图:
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
    view.addSubview(confirm)
}

问题在于子视图内的按钮根本没有响应。是什么原因造成的?我已经尝试过启用userInteraction。
谢谢

最佳答案

不确定是否适合或有帮助,但在我自己的情况下,我设置了类似的代码

pinView!.canShowCallout = true

然后,如果您想要一种简单的方法来捕获按钮单击,则将一个func放入ViewContriller中:
func buttonClicked (sender : UIButton!) {
    println("Button Clicked")
}

听起来您好像想做一些不同的事情。

在上一个答案中,我忘了我在按钮上方添加了按钮
return pinView

....
    let button : UIButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton
    button.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
    pinView!.rightCalloutAccessoryView = button
    return pinView

现在,buttonClicked函数可以捕获标注。

10-06 13:09
查看更多