A SwiftUI project demonstrating custom navigation handling using a Coordinator pattern with type-safe, hashable enum-based route management. This project explores how to push and pop views manually while passing data and completion handlers between screens.
This app consists of three views:
- ContentView (FirstView) – Initial screen with a text field and button to navigate.
- SecondView – Displays the text entered in the first view and allows navigation to the third view.
- ThirdView – Accepts input and passes data back to the second view using a completion handler.
The project leverages a custom Coordinator class to handle navigation actions and view building, all driven by the ViewDestination enum.
- Custom Coordinator pattern with SwiftUI's
NavigationStack - Type-safe enum-based routing (
ViewDestination) - Navigation with associated data and completion closures
- Reusable, environment-injected navigation logic
- Clear navigation hierarchy: push, pop, popToRoot
- Preview support for all views
- Start in FirstView (ContentView):
- Enter text → tap "Goto SecondView"
- SecondView:
- Displays first view's text
- Tap "Goto ThirdView" to input more text
- ThirdView:
- Enter text → tap "Dismiss this view"
- Text is passed back and displayed in SecondView
- Coordinator Class: Maintains a
NavigationPathand handles pushing/popping views. - ViewDestination Enum: Defines all possible destinations with associated data.
- @Environment(Coordinator.self): Views access coordinator for navigation actions.
- @Observable (Coordinator): Keeps navigation path in sync.
- Preview Support: All views use
.environment(Coordinator())for seamless previews.
