0

Im trying to use unwind segue to go back from a controller where users have to select an option:

In the main controller, users can touch a button that direct them to a second view controller where I programmatically build up buttons (the options). when the users select one of the buttons, I want them to go back to the previous controller and pass the button data along.

I can't find a way to make the unwind segue work.

here is what I have in the second view controller:

// Tile touch handler function when button pressed
    func selectedMood(sender:UIButton){
        println("touching tile")

        if(self.toShow == "mood"){
            self.mood = moodAre[sender.tag - 1]
        }
        else{
            self.action = actionAre[sender.tag - 1]
        }

        self.performSegueWithIdentifier("BackToPhraseSegue", sender: self)
    }

@IBAction func prepareForUnwind(segue: UIStoryboardSegue) {
        if (segue.identifier == "BackToPhraseSegue") {
            let destination = (segue.destinationViewController as! PhraseController)
            destination.mood = self.mood
            destination.action = self.action
            destination.place = self.place
        }
    }

    // Data transmit between controller
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if (segue.identifier == "BackToPhraseSegue") {
            let destination = (segue.destinationViewController as! PhraseController)
            destination.mood = self.mood
            destination.action = self.action
            destination.place = self.place
        }
    }

I linked the controller to the exit button (selecting the prepareForUnwind func) in the storyboard and I gave an identifier to the unwindSegue "BackToPhraseSegue"

What am I missing...

SKYnine
  • 2,708
  • 7
  • 31
  • 41

1 Answers1

2

Look at the second part of this answer to make sure you wired up the segue correctly.

How to wire up the exit segue so it can be called programmatically

The function you are unwinding to needs to be in the viewController you are returning to. Instead of prepareForUnwind, you need an unwindToHere in the previous viewController (the viewController you are returning to):

@IBAction func unwindToHere(segue: UIStoryboardSegue) {
    // And we are back
    let svc = segue.sourceViewController as! TheViewControllerClassYouAreReturningFrom
    // use svc to get mood, action, and place
}
Community
  • 1
  • 1
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • awesome! in fact I was trying to act from the wrong controller... thanks you very much! it wasn't clear enough in what I read on the subject. – SKYnine Aug 12 '15 at 00:12