Ios 以选择器swift发送对象

Ios 以选择器swift发送对象,ios,swift,selector,Ios,Swift,Selector,可以通过单击按钮发送多个对象吗 我正在尝试调用此函数 func getWeatherResults (lat: Double, long: Double{ } 通过单击viewFor上创建的按钮,从单击的注释中获取坐标 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { var **lat** = annotation.coordinate.

可以通过单击按钮发送多个对象吗

我正在尝试调用此函数

 func getWeatherResults (lat: Double, long: Double{

}
通过单击viewFor上创建的按钮,从单击的注释中获取坐标

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    var **lat** = annotation.coordinate.latitude
    var **long** = annotation.coordinate.latitude

    guard !(annotation is MKUserLocation) else { return nil }

    let annotationIdentifier = "Identifier"
    var annotationView: MKAnnotationView?
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
        annotationView = dequeuedAnnotationView
        annotationView?.annotation = annotation
    }
    else {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    }

    if let annotationView = annotationView {
        annotationView.canShowCallout = true

        let smallSize = CGSize(width: 30, height: 30)

        let krakenPinImg = UIImage(named: "kraken_ic")
        annotationView.image = krakenPinImg?.resizedImageWithinRect(rectSize: CGSize(width: 30, height: 30))

        let button = UIButton(frame: CGRect(origin: CGPoint.zero, size: smallSize))
        button.setBackgroundImage(UIImage(named: "weatherWindyDarkGray"), for: UIControlState())
        button.addTarget(self, action: #selector(getWeatherResults) for: .touchUpInside)
        annotationView.leftCalloutAccessoryView = button
    }


    return annotationView
}

谢谢

您可以为按钮创建自定义类。像这样:

class customButton: UIButton {
    var parameter : String?
}
将按钮类型设置为customButton并设置参数:

button.parameter = ""

无法自定义发送到按钮操作的参数。唯一有效的选项(如
UIControl
文档中所述)是具有参数、发送者(本例中的按钮)或发送者和事件

正确的解决方案是将坐标存储在特性中。然后,您可以根据需要在按钮处理程序中访问该属性

将属性添加到类中:

var lastCoordinate: CLLocationCoordinate2D?
更新地图视图代理方法:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    lastCoordinate = annotation.coordinate

    // and the rest of the code
}
并更新您的
getWeather
方法:

func getWeatherResults() {
    if let lastCoordinate = lastCoordinate {
        let lat = lastCoordinate.latitude
        let lon = lastCoordinate.longitude
        // Use these values as needed
    }
}

您的
getWeatherResults
方法的唯一有效参数是触发其调用的按钮(是的,我知道这不太正确,但对于这个问题来说足够接近)。向其发送坐标,然后获取
坐标。纬度
坐标。经度
供参考-这不是一个好的解决方案。每次需要访问数据时,对ui按钮进行子类化是一种糟糕的设计。视图不应包含数据。这违反了标准的模型-视图-控制器设计。感谢您消除我的疑虑!现在我知道我应该用什么了。