Swift 将变量从UIButton传递到另一个函数
如何将“坐标”变量传递给函数:DidClickDetailSwift 将变量从UIButton传递到另一个函数,swift,xcode,Swift,Xcode,如何将“坐标”变量传递给函数:DidClickDetail func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { guard annotation is MKPointAnnotation else { return nil } ... let rightButton = UIButton(type: .detailDisclosure)
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)
}
谢谢你的帮助 当使用@objC函数通过目标/操作发送参数时,该函数必须采用某种形式 基本上,它必须发送“发送者”或项目本身作为参数。在您的例子中,它将按钮作为参数发送,因为目标已添加到按钮
rightButton.tag = variable
因此,如果希望将坐标作为参数传递,可以创建一个存储坐标变量的自定义按钮类,并通过didclickdaildisposition方法传递该自定义按钮。您可以将rightButton设置为自定义按钮:
如果要传递的变量是
Int
type,则可以在按钮上设置标记
rightButton.tag = variable
您可以通过以下功能访问它:
@objc func didClickDetailDisclosure(button: UIButton) {
let variable = button.tag
}