Create Unwind Segues in Swift 3

Create Unwind Segues in Swift 3

This blog post is about how to create unwind segues in Swift. It?s super simple to implement them ? so of course, this post will be short. (Source code: )

But wait, what are the unwind segues?

Let?s say we have three screens ? 1,2 and 3. We want to move from 1 to 2 by creating a Show segue and now move to 3 from 2 by using a Present Modally segue in the Storyboard.

Image for post

We just created a Present Modally segue from 2 and 3? this means if we want to go back to 2 from 3, we can simply dismiss the present view controller(VC3) by using this method:

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

Familiar, isn?t it? But what if we want to go back to 1, not 2, from 3?

This is when unwind segues come in handy.

With unwind segues, you can now travel back to 1 from 3 without a need to go back to 2 first and then to 1 through the navigation controller.

Follow these simple four steps to create Unwind segues:

  1. In the view controller you are trying to go back to, VC1 in my example, write this code:

@IBAction func unwindToVC1(segue:UIStoryboardSegue) { }

( Remember: It?s important to insert this method in the view controller you are trying to go back TO! )

2. In the Storyboard, go to the screen you?re trying to unwind from ( 3 in our case ), and control + drag the view controller icon to the Exit icon located on top.

Image for post

As seen above, when you are presented with IBAction option(s) to connect to, select the unwind segue action you just created in VC1.

3. Go to the document outline of the selected view controller in the Storyboard, select the unwind segue as shown below.

Image for post1. Select the Unwind segue in the document outline

Now, go to the Attributes Inspector in the Utilities Pane and name the identifier of the unwind segue.

Image for post2. Specify the identifier of the Unwind segue

4. Finally, write this code where you want the unwind segue action to be triggered, V3 in our case.

@IBAction func goBackToOneButtonTapped(_ sender: Any) { performSegue(withIdentifier: “unwindSegueToVC1”, sender: self)}

The source file for this tutorial can be found here:

Happy Coding!


No Responses

Write a response