SKYnine
SKYnine

Reputation: 2708

performSegue after completion handler

ANSWER BELOW

Im facing a little issue that you may help me with.

the app Im working on allows you to request for content based on your location.

the first ViewController is somewhat a form that grab your location / a specified location + some other information to target specific answers.

I need to perform a segue to pass the "question" variables to my second ViewController where I load "answers" with a query based on the question details.

What is causing me trouble is that, whenever the question is geolocalized, I can't retrieve the information using prepareForSegue because it doesn't wait for the geoPoint to be made (completed).The second controller display my latitude and longitude as nil.

I see that I can call the "prepareForSegue" method using "perfomSegueWithIdentifier", and retrieve the information in my second view controller but it perform the segue twice... How can I trigger the segue only when Im ready but using the prepareForSegue data parameter I need to preserve?

Is there a way to pass variable from one controller to another using performSegue? Any help would be awesome.

Also, while I don't think the code is relevant for my question, here is the code I use.

geoPointing method

@IBAction func doPostQuestion(sender: UIButton) {

        var thereQ:PFObject = PFObject(className: "tquestion")

        if(somewhereLabel.text == "my location"){
            println("Location is geolocalized")
            PFGeoPoint.geoPointForCurrentLocationInBackground {
                (geoPoint: PFGeoPoint!, error: NSError!) -> Void in
                if error == nil {
                    self.geoLati = geoPoint.latitude as Double
                    self.geoLong = geoPoint.longitude as Double
                    self.performSegueWithIdentifier("goto_results", sender:self)  // call prepareForSegue when ready but implies to have a segue done on click... (performed twiced)
                }
            }

        }
        self.navigationController?.popToRootViewControllerAnimated(true)
    }

prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if(segue.identifier == "goto_results"){

                // Get Label
                let theDestination = (segue.destinationViewController as displayAnswersViewController)
                theDestination.lat = self.geoLati
                theDestination.lng = self.geoLong
        }
    }

ANSWER SOLUTION:

As suggested, to solve this problem you just need to create your segue from your viewController1 to your viewController2 and not from a button. This way you can trigger prepareForSegue programatically using the "performSegue" method that will call prepareForSegue anyway.

Upvotes: 0

Views: 2677

Answers (1)

linimin
linimin

Reputation: 6377

To solve this problem you just need to create your segue from your viewController1 to your viewController2 and not from a button. This way you can trigger prepareForSegue programatically using the "performSegue" method that will call prepareForSegue anyway.

Upvotes: 1

Related Questions