SwiftUI navigation with MVVM-C
Follow a category tap in Pose from the ViewModel to the photo grid through the Coordinator and FlowStack.
When someone taps a category on the Home screen in Pose, the app opens a photo grid and passes along the selected category. I use that small flow to explain how I organize navigation in a SwiftUI app with several screens. The ViewModel owns the navigation decision. The Coordinator names destinations and defines the data each one needs.
SwiftUI and MVVM in a minute
SwiftUI describes what a screen should look like for its current state. When the data a View observes changes, SwiftUI updates the relevant part of the interface. In MVVM, the Model represents data, the View displays it and forwards user actions, and the ViewModel holds screen state and handles those actions.
The C in MVVM-C stands for Coordinator. In Pose, each screen’s Coordinator declares its routes. A route is a Swift enum case with associated values when a destination needs them. A Navigator performs operations on the stack, and the View registers what SwiftUI should show for each route. That is how Pose divides the responsibilities.
Where a single tap goes
On Home, selecting a category calls viewModel.openPhotoGridView(category). The ViewModel sends a .photoGrid route to the navigator. These excerpts keep the relevant navigation code.
// HomeView
Button(category.title) {
viewModel.openPhotoGridView(category)
}
// HomeView.ViewModel
func openPhotoGridView(_ category: CategoryModel) {
navigator.push(
Coordinator.Navigation.photoGrid(
category,
categories,
isSearching: false,
isFromCamera: false
)
)
}
The active category and the full category list travel with the route. Two flags capture the context of entering from Home. At this point, I can see which screen the ViewModel selects and what context it hands over.
From a route to a screen
HomeView.Coordinator.Navigation declares the .photoGrid case. HomeView registers a .flowDestination for the enum. When it receives the route, it pulls out the associated values and uses them to build PhotoGridView with its ViewModel.
// Relevant branch in the .flowDestination switch
case let .photoGrid(category, categories, isSearching, isFromCamera):
PhotoGridView(
viewModel: .init(
category: category,
categories: categories,
isSearching: isSearching,
isFromCamera: isFromCamera
)
)
At the root, RootView creates FlowStack($viewModel.path, withNavigation: true). The stack uses the path to present screens as the Navigator updates routes. A tap travels from the View through a named ViewModel method, then from a typed route to a SwiftUI screen.
What I watch as it grows
The enum surfaces changes early. Add a case and an exhaustive destination switch needs to account for it. A ViewModel can also reuse the same route from several entry points and vary the context it sends. To test that decision without launching the UI, I would make the push operation replaceable with a navigator that records the route.
This structure adds a declaration and a mapping. It earns its place when several screens share a destination or when each entry point needs to explain its context. If a case starts carrying several models or flags with unclear relationships, I revisit whether a stable ID or a named context type would express the flow more clearly.
Follow on LinkedIn