In iOS 5, we were introduced to Storyboards and Segues. While these features simplified the navigation process in our apps, we weren’t given a way to define a return to previous modal scenes via Interface Builder.
You may have noticed that in iOS 6, a green icon has been added to the control bar of each scene in the Storyboard.
This is a new, and surprisingly overlooked feature for iOS 6, which provides the missing “return” functionality. It’s called an “unwind segue.”
The process is simple:
- Add a private method inside each View Controller you wish to “unwind” to (a common mistake is to add this code into the View Controller you wish to unwind from).
- This method includes one (and only one) parameter that’s a pointer to a UIStoryboardSegue, and a return type of IBAction .
- The name of this method can be whatever you’d like. However, all of the unwind segues will be listed in one place,so it’s a good idea to pick a descriptive naming pattern, such as unwindToRootVC.
- There is no need to have anything in the body of this method. The signature is what’s important.
- Inside your Storyboard, hold the control key down while dragging from your button (or other control) to the green exit icon below.
- You will see a list of every unwind segue available here… that’s why coming up with a consistent and descriptive naming pattern is important.
That’s it! You are now able to return to any previous scene simply by selecting the appropriate unwind segue. Also, unlike following a ‘regular’ segue, this will use not create a new object for every request!