Unwind Segues Effect

As in Mass Effect… anyone? OK, so yesterday on my way home from CodeFellows HQ I was going through the list of things I wanted to read and came across an article I saved a while back called “Unwinding”. The title sounded interesting and since I had nothing better to do on my 40 min bus ride, I decided to give it a read.

Typically when you present a view controller (modally), presenting view controller is set to be the delegate and is responsible for dismissing it when the user is done performing his / her task. While it is possible to dismiss presented view controller from within itself, that’s not the pattern to pursue.

Traditional way of handling the dismissal of the presented view controller looks something like this:

Presenting view controller has to implement the delegate like so:

Not a big deal, super simple and takes about 2 minutes to setup. It becomes more complex when you try to set up your view controller to be presented modally for adding new information and then use push segue to display the information.

Without going into details of above mentioned setup, I would like to show you how you can use segue unwind feature to dismiss modal view controller without writing a protocol.

Let’s assume you have a HomeViewController that at some point of its lifecycle gets a chance to present DetailViewController with a push of a button. You add the button to the HomeViewController, wire it up to action and let the segue take care of the rest. To dismiss it using unwinding feature, you would do the following:

In your HomeViewController or the presenting view controller (whichever makes more sense to you), you simply declare and implement the following method:

Then, in your storyboard drag from the button on your DetailViewController to the Exit icon and choose:


That’s it. The dismissal of the DetailViewController will be handled by this segue. Make sure you set the identifier on your unwind segue by opening up Document Outline panel, selecting “Unwind segue”, and adding the identifier in Attributes Inspector.

Pretty cool, huh?

Unwind Segues Effect