首页 > 解决方案 > 目标函数未被调用 UIButton

问题描述

我有一个看起来很好的自定义标注,如下所示:

在此处输入图像描述

但是由于某种原因,当我点击任一按钮时,没有调用目标函数,也不知道为什么。以下是我正在尝试的:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    if view.annotation is MKUserLocation
    {
        return
    }
    let calloutAnnotation = view.annotation as! StopAnnotation
    selectedStopAnnotation = calloutAnnotation
    let calloutView = UIView(frame: CGRect(x: 0, y: 0, width: 147, height: 155))
    calloutView.backgroundColor = .darkGray

    let fromButton = UIButton(frame: CGRect(x: 22.0, y: 20.0, width: 103.0, height: 44.0))
    fromButton.backgroundColor = .white
    fromButton.setTitle("+ From", for: .normal)
    fromButton.setTitleColor(.darkGray, for: .normal)
    fromButton.addTarget(self, action: #selector(self.fromButtonAction), for: .touchUpInside)
    calloutView.addSubview(fromButton)

    let toButton = UIButton(frame: CGRect(x: 22.0, y: 91.0, width: 103.0, height: 44.0))
    toButton.backgroundColor = .white
    toButton.setTitle("+ To", for: .normal)
    toButton.setTitleColor(.darkGray, for: .normal)
    toButton.addTarget(self, action: #selector(self.toButtonAction), for: .touchUpInside)
    calloutView.addSubview(toButton)

    calloutView.center = CGPoint(x: view.bounds.size.width / 2, y: -calloutView.bounds.size.height*0.52)
    view.addSubview(calloutView)
    mapView.setCenter((view.annotation?.coordinate)!, animated: true)
}

@objc func fromButtonAction(sender: UIButton) {
    fromTextField.text = selectedStopAnnotation.title
}

@objc func toButtonAction(sender: UIButton) {
    toTextField.text = selectedStopAnnotation.title
}

标签: iosswiftuibutton

解决方案


推荐阅读