xcode 8 swift: unwind segue transfer data is not working

645 views Asked by At

So I have been trying to get a unwind segue to work from last two days. I have tried watching and follow several different tutorials and none was successful. I created the @IBAction override func unwind(for unwindSegue: UIStoryboardSegue, towardsViewController subsequentVC: UIViewController) but this never performs any of the code. I put a simple print and had it print text, it never printed the text. I created that in the first View Controller then in the second View Controller I had a button that I connected to the Exit at the top of the View Controller and selected the unwind action and it never unwinds. What am I doing wrong or missing?

So here is what I have so far I have 2 views: RaceViewController & SingleStatViewController

I will start by selecting specified rows in RaceViewController and they will open SingleStatViewController modally. I have a button in SingleStatViewController called dismissBtn. When I click the button it dismisses SingleStatViewContoller. I just need it to pass some data back to the RaceViewController.

So if the user selects 1 of 3 cells in the table (the rows are Half-Orc, Half-Elf and Human) it will open SingleStatViewController and ask to select 1 stat (these will be of 6 buttons (Str, Dex, Con, Int, Wis, Chr) when they select the one of the buttons it will return the data and update the detailTextLabel.

I select Human then I select Str. When it gets back to RaceViewController and it updates the text in the detailTextLabel to +2 Str.

Right now, this is the code I am using to dismiss. Can I use this to continue or do I actually need to perform the unwind:

@IBAction func dismissbtn(_ sender: Any)
{
    dismiss(animated: true, completion: nil)
}
2

There are 2 answers

0
Ashutosh Dave On BEST ANSWER

You need to perform unwind and send data as following:

@IBAction func unwindToRaceViewController(sender: UIStoryboardSegue) 
{
    if let sourceViewController = sender.sourceViewController
                            as? RaceViewController {
    stat = sourceViewController.stat
}

You could refer to this article for unwind segues as alternative to delegate

You could also use delegates to pass data back to the previous controller.

3
Tj3n On

Okay, so you have vc A and vc B, this is what you need to do to use unwind segue:

  1. In vc A, create a segue function eg. @IBAction func unwind(segue: UIStoryboardSegue)

  2. In vc B, write code that call self.performSegueWithIdentifier("unwind", sender: self) (assume that the identifier and the function name is the same - call this in your dismissbtn)

  3. In vc B in storyboard, look on top of it, there will be an yellow button and 2 orange button, hold and drag the yellow button to the Exit orange button, choose the action that you created in vc A, give it an identifier as mentioned

  4. In the unwind func, the rest is the same as normal segue when you pass data forward, just change destination to source and you must present vc B from vc A, else then use delegate