In https://developer.apple.com/wwdc22/10054 it is shown that the best way to organize an iPad layout is by:
struct TwoColumnContentView: View {
@Binding var showExperiencePicker: Bool
@EnvironmentObject private var navigationModel: NavigationModel
var categories = Category.allCases
var dataModel = DataModel.shared
var body: some View {
NavigationSplitView(
columnVisibility: $navigationModel.columnVisibility
) {
List(
categories,
selection: $navigationModel.selectedCategory
) { category in
NavigationLink(category.localizedName, value: category)
}
.navigationTitle("Categories")
.toolbar {
ExperienceButton(isActive: $showExperiencePicker)
}
} detail: {
NavigationStack(path: $navigationModel.recipePath) {
RecipeGrid(category: navigationModel.selectedCategory)
}
}
}
}
NavigationModel defined as
final class NavigationModel: ObservableObject, Codable {
@Published var selectedCategory: Category?
@Published var recipePath: [Recipe]
@Published var columnVisibility: NavigationSplitViewVisibility
}
But what happens when in a child view, it triggers off some action in some other ViewModel that needs to update the navigation stack?
struct ChildViewModel {
func childViewCalledAndDidSomething {
//now I need to update recipePath, how?
}
}