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.
//Holding the name of the controller from where the segue is coming from var segueFromController : String! @IBAction func unwindSegue(sender: UIButton) { }
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
class FirstViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func backToFirstViewController(storyboard: UIStoryboardSegue){ } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { let destination = segue.destinationViewController as! LastViewController destination.segueFromController = "FirstViewController" } }
SecondViewController.swift
class SecondViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func backToSecondViewController(storyboard: UIStoryboardSegue){ } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { let destination = segue.destinationViewController as! LastViewController destination.segueFromController = "SecondViewController" } }
ThirdViewController.swift
class ThirdViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func backToThirdViewController(storyboard: UIStoryboardSegue){ } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { let destination = segue.destinationViewController as! LastViewController destination.segueFromController = "ThirdViewController" } }
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.
@IBAction func unwindSegue(sender: UIButton) { if segueFromController == "FirstViewController"{ self.performSegueWithIdentifier("backToFirstViewController", sender: nil) } else if segueFromController == "SecondViewController"{ self.performSegueWithIdentifier("backToSecondViewController", sender: nil) } else if segueFromController == "ThirdViewController"{ self.performSegueWithIdentifier("backToThirdViewController", sender: nil) } }
That’s it. Now run and test it. It must be working fine.
Source code for this project is available on Github.
Salut!
This article covers some important things you must know when you are considering a move…
What is Unit Testing? In its simplest term, unit testing is testing a small piece…
In this article, you will learn about a type of Creational Design Pattern which is…
In this tutorial, you will go through the use of targets to achieve two separate…
In this article, you will learn about a type of Structural Design Pattern which is…
In this article you will learn about a type of Creational Design Pattern which is…
View Comments
Thanks. Great tutorial.
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.
Can you show me your code?