SwiftPumpkin

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

Creates, pushesonNextCreates, pushesonFinishCoordinatorOwns the flowScreen AReports intentScreen BReports intent
calls or tellsreports backScreens never create or push each other. The coordinator decides what comes next.
  • A coordinator owns a UINavigationController and the flow.
  • Each screen exposes callbacks like onNext instead of pushing other screens.
PartOwnsLooks like
CoordinatorCreate screens, decide flow, own navigationClass holding UINavigationController
ScreenDisplay and report intentUIViewController with closures
Child coordinatorsSub-flows with their own lifetimeArray 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 flowsOnboarding, checkout, this thank-you wizard.
Screens reused from several placesA coordinator per flow, the screen unchanged.
One list and one detailNot needed yet.

Compared

Segues vs coordinator vs SwiftUI path

SeguesCoordinatorPath
Who picks the next screenSource VCCoordinatorCode mutating path
Reuse a screen in another flowHardEasyEasy
Deep linksManual chainsBuild stack from routeAssign the path
Testing navigationUI testsUnit testsUnit 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.

30-second answer

Explain Coordinator / Routing in 30 seconds.

iOS codingAsked directly

What problem does the Coordinator pattern solve, and how do child coordinators stay alive and get cleaned up?

iOS codingAsked indirectly

This detail screen must open from two different flows. How do you avoid it knowing where it came from?

System designAsked indirectly

An edit-profile screen needs to save via an API call. Does the POST belong in the view controller?

Read it in the app's source