Ios 如何在geocodeAddressString之后通过segue传递数据

Ios 如何在geocodeAddressString之后通过segue传递数据,ios,swift,segue,grand-central-dispatch,geocoding,Ios,Swift,Segue,Grand Central Dispatch,Geocoding,我试图通过segue获取从一个视图控制器传递到另一个视图控制器的地址坐标。获取坐标的地理编码函数异步运行,因此我使用一个完成块来捕获坐标值 编辑:单击按钮可触发以下功能- func getCoordinates(completion: (coordinates)) -> () { geocoder.geocodeAddressString(address) { (placemarks, error) -> Void in if((error) != nil)

我试图通过segue获取从一个视图控制器传递到另一个视图控制器的地址坐标。获取坐标的地理编码函数异步运行,因此我使用一个完成块来捕获坐标值

编辑:单击按钮可触发以下功能-

func getCoordinates(completion: (coordinates)) -> () {
    geocoder.geocodeAddressString(address) { (placemarks, error) -> Void in
        if((error) != nil) {
            print("Error", error)
        }
        if let placemark = placemarks?.first {
            let coordinates: CLLocationCoordinate2D = placemark.location!.coordinate

    completion(coordinates)

        }
    }
}
我要做的是在获得坐标后,将坐标传递给下一个视图控制器。我怀疑我可以用prepareForSegue和GCD做到这一点,但我可能错了

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showCoordinates" {
        if let nextVC = segue.destinationViewController as? NextViewController {

            // What goes here?
        }
    }
}

我需要一些帮助/建议。提前感谢。

NextViewController
上创建一个属性,以接受该数据并将其分配给目标视图控制器:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showCoordinates" {
        if let nextVC = segue.destinationViewController as? NextViewController {
            nextVC.coordinates = coordinates
        }
    }
}

NextViewController
上创建属性以接受该数据并将其分配给目标视图控制器:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showCoordinates" {
        if let nextVC = segue.destinationViewController as? NextViewController {
            nextVC.coordinates = coordinates
        }
    }
}
prepareforsgue
方法中,可以将发送方对象强制转换为
CLLocationCoordinate2D
对象,并将其分配给下一个ViewController的
CLLocationCoordinate2D
变量

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showCoordinates" {
        if let nextVC = segue.destinationViewController as? NextViewController {
            nextVC.coordinates = sender as! CLLocationCoordinate2D
        }
    }
}
prepareforsgue
方法中,可以将发送方对象强制转换为
CLLocationCoordinate2D
对象,并将其分配给下一个ViewController的
CLLocationCoordinate2D
变量

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showCoordinates" {
        if let nextVC = segue.destinationViewController as? NextViewController {
            nextVC.coordinates = sender as! CLLocationCoordinate2D
        }
    }
}

谢谢,Kyle,但我无法从getCoordinates函数中获取坐标或将其分配给全局变量,因为geocodeAddressString是异步运行的。
geocodeAddressString
是否在执行segue后返回?谢谢,Kyle,但我无法从getCoordinates函数中获取坐标或将其分配给全局变量,因为geocodeAddressString异步运行。
geocodeAddressString
是否在执行segue后返回?嗨,Ankahathara。你是不是建议我不要使用完成块,而是让“坐标”变量触发segue?嗨,Ankahathara。你是不是建议我不要使用completion块,而是让“coordinates”变量触发segue?