Skip to content
tezvyn:
Menu

Unwind Segues: Jump Back in the Stack

MediumHow cards are made

An unwind segue jumps from a deep screen to an earlier view controller without manual popping. Use it after multi-step flows to return to a root screen. If multiple screens share the same action, UIKit picks the nearest one and silently routes users wrong.

Why it exists

Standard navigation in UIKit moves forward through push and present transitions, but going back usually means popping one level or dismissing a single modal. When a user finishes a task several screens deep, manually chaining popViewController or dismiss calls across multiple view controllers creates brittle glue code. Unwind segues let the storyboard declare a backward destination declaratively, so the framework handles the traversal.

The mental model

Think of an unwind segue as a labeled emergency exit placed on an upstream view controller. Any downstream screen can pull the handle, and UIKit walks backward through the navigation stack or modal hierarchy until it finds the matching exit sign. If no sign exists, the door simply does not open.

How it works

You define an action method on the destination view controller using the signature func unwindActionName with a UIStoryboardSegue parameter. In Interface Builder, you connect a segue from a button or trigger in the source scene to the Exit icon, selecting that method. At runtime, UIKit searches the presented view controllers or navigation stack in reverse order, looking for the first view controller that responds to the selector. It then performs the unwind, calling prepareForSegue on the source, executing the unwind action on the destination, and removing every intermediate view controller automatically.

When to use it

Use unwind segues when you need to return to a specific ancestor after completing a multi-step flow, such as finishing a checkout wizard, submitting a form spread across several screens, or canceling a deep modal presentation. They keep backward navigation visible in the storyboard instead of hidden in code.

When not to use it

Avoid them in programmatic UI or coordinator-based architectures where storyboards are absent, because unwind segues require Interface Builder and segue identifiers. They also become risky when the navigation stack is dynamic or contains multiple view controllers that share the same unwind selector name; UIKit always picks the nearest match, which can route the user to an unexpected screen without warning.

One canonical example

Imagine a navigation stack with Account List, then Account Detail, then Edit Account, then Confirm Changes. After the user taps Save on Confirm Changes, you want to jump directly back to Account List, skipping the intermediate screens. You implement an unwindToAccountList method on Account List, draw an unwind segue from the Save button in Confirm Changes to the Exit placeholder, and UIKit unwinds the entire stack in one transition.

Interview question

If multiple view controllers in the navigation stack implement the same unwind action method name, how does UIKit behave at runtime?

  • a.UIKit routes to the nearest matching view controller, which may not be the intended destinationCorrect
  • b.UIKit unwinds all the way to the root view controller regardless of which screens implement the method
  • c.UIKit ignores the duplicate methods and cancels the unwind transition
  • d.UIKit throws a runtime exception because the selector is ambiguous
Why?

UIKit searches backward through the stack and silently unwinds to the first view controller that responds to the selector, which can send the user to the wrong screen. This makes the runtime exception distractor tempting, but the framework does not crash on duplicate names.

Just read this? Test yourself on what you have been reading.

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.

See open roles