Press "Enter" to skip to content

Multiple Unwind Segue from one ViewController

Greetings everyone!

I hope you all would be doing great.
I wanted to do a small tutorial on how to Exit(Unwind Segue) a controller when more than one segues are directed towards one ViewController and how to keep track of which one to go back to.

Here I have 5 View Controllers(Let’s say, MainViewController, FirstViewController, SecondViewController, ThirdViewController, and LastViewController). I have three buttons on initial Controller to go to other three Controllers and every other Controller has a button to go to the Last View Controller, and on Last View Controller there is also a button to go back to its respective Controller. (Don’t forget to embed in navigation in the initial View Controller)

Screen Shot 2016-04-17 at 11.28.50 AM

Open class of your LastViewController and create a string variable and the action function for the button.

Then in your all the Controllers(First, Second, and Third) create @IBAction functions with arguments of type UIStoryboardSegue, and also override prepareForSegue in each ViewController.
Your Controllers should look something like this.

FirstViewController.swift

 

SecondViewController.swift

 

ThirdViewController.swift

 

After this being done, come back to the storyboard and on LastViewController click the yellow circle and right click + drag it towards Exit circle.

Screen Shot 2016-04-17 at 11.48.13 AM

Screen Shot 2016-04-17 at 11.48.57 AM

Select backToFirstViewController. Then open Document Outline select the newly created segue and give it an identifier. Repeat this step for all the three ViewControllers. (I gave it as the function’s name for simplicity)

Screen Shot 2016-04-17 at 11.50.40 AM

Finally in the @IBAction of your LastViewController. Do the following.

That’s it. Now run and test it. It must be working fine.

Source code for this project is available on Github.

Salut!

Author: kodesnippets

3 Comments

  1. Garcia
    Garcia May 19, 2016

    Thanks. Great tutorial.

    • Joseph
      Joseph December 28, 2016

      Thanks for the tutorial. However, I keep getting “fatal error: unexpectedly found nil while unwrapping an Optional value here” at this line: @IBAction func unwindSegue(sender: UIButton) { if segueFromController == “FirstViewController…” Any insight would be greatly appreciated.

      • Aaqib Hussain
        Aaqib Hussain January 1, 2017

        Can you show me your code?

Leave a Reply

Your email address will not be published. Required fields are marked *