6 of 22 · UI patterns
Coordinator / Routing
A coordinator is an object that owns navigation for a flow. Screens report what happened ("user picked a pumpkin") and the coordinator decides what comes next, so view controllers never create or push each other.
How it's wired
- A coordinator owns a UINavigationController and the flow.
- Each screen exposes callbacks like onNext instead of pushing other screens.
| Part | Owns | Looks like |
|---|---|---|
| Coordinator | Create screens, decide flow, own navigation | Class holding UINavigationController |
| Screen | Display and report intent | UIViewController with closures |
| Child coordinators | Sub-flows with their own lifetime | Array in parent |
What makes coordinators hard
- Lifetime. A parent must retain its child coordinators and release them when a child finishes. Back-swipe and interactive modal dismissal bypass your finish callback, so you also need UINavigationControllerDelegate or the presentation controller's delegate, or the child leaks.
- Deep links. Navigation becomes "set state, then start": a coordinator should be able to build any valid stack from a route, not only push one step at a time.
- In SwiftUI, NavigationStack(path:) makes navigation a value. The coordinator becomes an @Observable router that owns the path, which is easy to unit test and restore.
When I'd use a coordinator
| Multi-step flows | Onboarding, checkout, this thank-you wizard. |
|---|---|
| Screens reused from several places | A coordinator per flow, the screen unchanged. |
| One list and one detail | Not needed yet. |
Compared
Segues vs coordinator vs SwiftUI path
| Segues | Coordinator | Path | |
|---|---|---|---|
| Who picks the next screen | Source VC | Coordinator | Code mutating path |
| Reuse a screen in another flow | Hard | Easy | Easy |
| Deep links | Manual chains | Build stack from route | Assign the path |
| Testing navigation | UI tests | Unit tests | Unit tests |
Expert notes
- A good interview signal: a screen should be usable from two flows without edits. If it can't be, something is pushing it from the wrong place.
Check yourself
Answer out loud before you reveal. Recall is what makes it stick.
Explain Coordinator / Routing in 30 seconds.
- A coordinator owns navigation for a flow.
- Screens report intent, like “user picked a pumpkin”, and the coordinator decides what's next, builds it and pushes it.
- Screens never know what comes after them, so one screen works in two flows and deep links become “build this stack”.
- The cost is lifetime management: parents retain child coordinators and must release them, including on back-swipe.
What problem does the Coordinator pattern solve, and how do child coordinators stay alive and get cleaned up?
- It stops view controllers from building and presenting the next screen, which welds them to one flow.
- A coordinator is a plain object that owns the flow instead.
- Parents keep children in
var childCoordinators: [Coordinator]so they stay alive. - When a child finishes, it tells its parent through a delegate, and the parent removes it.
- Forget that removal and you leak whole flows.
This detail screen must open from two different flows. How do you avoid it knowing where it came from?
- Have the screen report intent instead of navigating.
- A view controller should manage either the sequence of screens or its own UI, never both.
- The list turns the row into a model and calls
delegate?.didSelect(item). - A parent flow controller that owns the navigation stack decides what comes next.
- Either flow can then host the same detail screen unchanged.
An edit-profile screen needs to save via an API call. Does the POST belong in the view controller?
- No, and not in the coordinator either.
- The view controller reports "save tapped" with the edited values.
- The view model or an interactor performs the save through a repository.
- The coordinator only decides what happens next: dismiss, or show login if the session expired.
- Keeping "who changes data" apart from "who navigates" stops the coordinator becoming a god object.