1

I'm trying to get the coordinates of an address passed from one view controller to another via a segue. The geocoding function to get the coordinates runs asynchronously, so I'm using a completion block to capture the coordinate values.

EDIT: The function below is triggered by clicking a button-

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)

        }
    }
}

What I'm trying to do is pass the coordinates to the next view controller AFTER they've been obtained. I suspect I can do this with prepareForSegue and GCD but I could be wrong....

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

            // What goes here?
        }
    }
}

Could use some help/suggestions. Thanks in advance.

Wertever
  • 45
  • 5

2 Answers2

0

Create a property on NextViewController to accept that data and assign it to the destination view controller:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showCoordinates" {
        if let nextVC = segue.destinationViewController as? NextViewController {
            nextVC.coordinates = coordinates
        }
    }
}
Kyle Redfearn
  • 2,172
  • 16
  • 34
  • Thanks, Kyle but I can't get the coordinates out of the getCoordinates function or assign them to a global variable because geocodeAddressString runs asynchronously. – Wertever Feb 17 '16 at 04:05
  • Does the `geocodeAddressString` return after the segue has been performed? – Kyle Redfearn Feb 17 '16 at 04:10
0
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)
   self.performSegueWithIdentifier("showCoordinates", sender: coordinates)
        }
    }
}

and in your prepareForSegue method you can cast the sender object in to CLLocationCoordinate2D object and assign in to your nextViewController's CLLocationCoordinate2D variable.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showCoordinates" {
        if let nextVC = segue.destinationViewController as? NextViewController {
            nextVC.coordinates = sender as! CLLocationCoordinate2D
        }
    }
}
Ankahathara
  • 2,866
  • 2
  • 20
  • 26
  • Hi, Ankahathara. Are you suggesting that I not use the completion block and just make the "coordinates" variable trigger the segue instead? – Wertever Feb 17 '16 at 06:26