目标函数未称为UIButton

时间:2018-12-04 03:17:42

标签: ios swift uibutton

我有一个自定义标注,看起来像这样:

enter image description here

但是由于某些原因,当我点击任一按钮时,未调用目标函数,也不确定原因。以下是我正在尝试的:

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
}

0 个答案:

没有答案