Create Unwind Segues in Swift 3

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.

 

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.

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.

1. 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.

2. 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)
}

Happy Coding!

Advertisements
Create Unwind Segues in Swift 3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s