如何将“坐标”变量传递给函数:didClickDetailDisclosure吗?
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is MKPointAnnotation else { return nil }
...
let rightButton = UIButton(type: .detailDisclosure)
let coordinates = annotation.coordinate
rightButton.
rightButton.addTarget(self, action: #selector(didClickDetailDisclosure(button:)), for: .touchUpInside)
annotationView?.rightCalloutAccessoryView = rightButton
@objc func didClickDetailDisclosure(button: UIButton) {
performSegue(withIdentifier: "SegueAddFromMaps", sender: self)
}
感谢帮助!
答案 0 :(得分:0)
使用@objC函数通过目标/操作发送参数时,该函数必须采用某种形式。
基本上,它必须发送“发件人”或项目本身作为参数。在您的情况下,因为将目标添加到了按钮,所以它将按钮作为参数发送。
因此,如果您要将坐标作为参数传递,则可以创建一个存储Coordinates变量的自定义按钮类,然后通过didClickDetailDisclosure方法传递该自定义按钮。您可以将rightButton设置为自定义按钮:Passing arguments to selector in Swift
class CustomButton : UIButton {
var coordinate: CLLocationCoordinate2D?
convenience init(coordinate: CLLocationCoordinate2D, object: Any) {
self.init()
self.coordinate = coordinate
}
}
@objc func didClickDetailDisclosure(button: CustomButton) {
let coordinate = button.coordinate
performSegue(withIdentifier: "SegueAddFromMaps", sender: self)
}
答案 1 :(得分:0)
如果您要传递的变量是Int
类型,则可以在按钮上设置标签。
rightButton.tag = variable
您可以在类似的功能中访问它
@objc func didClickDetailDisclosure(button: UIButton) {
let variable = button.tag
}