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.
1 2 3 4 5 6 |
//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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
@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!
3 Comments