Unwind segues
Published in
2 min readApr 11, 2020
What is an unwind segue?
An unwind segue is a way to navigate back through other segues as if you were popping, dismissing, or closing the view controllers that got you to where you are.
Why/when to use an unwind segue?
- If you need to jump back more than one view controller: If you have a hypothetical VC1 that opens VC2 (that may contain a list of categories). That VC2 then opens VC3 that has the data you want to select. In order to go back directly to VC1 from VC3, you use an unwind segue.
- If a view controller is reachable in more than one way: You may often have different methods to navigate to a view controller, such as a navigation controller, or a modal presentation. Using unwind segues, removes the responsibility of the view controller to know the method of getting back to where it started, which is not a responsibility it should have.
- It is good practice to be consistent in your coding: If you are using segues to go to different view controllers, you should use the counterpart, unwind segues. It keeps your code consistent, and makes it more readable.
4 steps to creating an unwind segue
1 — Create a function in the view controller you want to go back to.
2 — create “back” button
Add a button to the view controller that will go back to the originating VC.
3 — link button to the current VCs exit (control click and drag)
4 — Select unwind segue from action list
Conclusion
There you have it. The quick and simple process to create an unwind segue.