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)

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.

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)

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!

Share:

administrator

3 Comments

  • Garcia, May 19, 2016 @ 8:39 am Reply

    Thanks. Great tutorial.

    • Joseph, December 28, 2016 @ 3:18 am Reply

      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, January 1, 2017 @ 1:13 pm Reply

        Can you show me your code?

Leave a Reply

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