SwiftUI Navigation Issue: Handling Navigation Across Multiple Tabs with Separate Navigation Stacks

I'm working on a SwiftUI application that uses programmatic navigation with enums in a NavigationStack. My app is structured around a TabView with different tabs, each having its own navigation stack. I need to navigate to the same view from different tabs, but each tab has a separate navigation stack with custom paths.

Here's a simplified version of my setup:

class AppState: ObservableObject {
    @Published var selectedTab: Tab = .feeds
    
    @Published var tabANavigation: [TabANavDestination] = []
    @Published var tabBNavigation: [TabBNavDestination] = []
}

enum TabANavDestination: Hashable {
    case itemList()
    case itemDetails(String)
}

enum TabBNavDestination: Hashable {
    case storeList()
    case itemList()
    case itemDetails(String)
}

For example:

TabA: ItemsListScreen -> DetailsScreen.

TabB: StoreList -> ItemsListScreen -> DetailsScreen

I want to navigate from ItemsListScreen to DetailsScreen, but I can't use the same method to append the navigation state in both tabs:

appState.tabANavigation.append(.itemDetails(id))


How can I manage navigation across these different tabs, ensuring that the same screen (e.g., DetailsScreen) is accessible from different paths and tabs with their own navigation stacks? What’s the best approach to handle this scenario in a complex navigation setup?

Answered by DTS Engineer in 799786022

@faizy Please review Enhancing your app’s content with tab navigation Sample project. It contains best practices that would help guide you.

You certainly can navigate to DetailsScreen simultaneously on multiple navigation stacks. Each DetailsScreen would then be a separate instance. Did you run into any actual issue, or is this a conceptual question?

@faizy Please review Enhancing your app’s content with tab navigation Sample project. It contains best practices that would help guide you.

SwiftUI Navigation Issue: Handling Navigation Across Multiple Tabs with Separate Navigation Stacks
 
 
Q