1

I'm trying to implement Pop to Root functionality in an app with a Tab View.

Here's my class where I define my Navigation Paths. Each path is assigned to a Tab. Tabs share child views that have a button that calls the reset() function. Right now "featuredPath" is hard coded in the function. What I'd like would be for the function to switch on the Path name and reset the correct one.

In order for me to do that, I would need the child view to return the active Path name when the button is tapped.

Is this possible?

class Router: ObservableObject {
    @Published var featuredPath = NavigationPath()
    @Published var favoritePath = NavigationPath()
    @Published var searchPath = NavigationPath()
    func reset() {
        featuredPath = NavigationPath()
    }
}
struct MainViewScreen: View {
    @EnvironmentObject var router: Router

    
    var body: some View {
        
        TabView {
            NavigationStack(path: $router.featuredPath) {
                FeaturedContainer()
            }
            .tabItem {
                Label("Featured", systemImage: "star.circle")
            }
            NavigationStack(path: $router.favoritePath) {
                FavoritesContainer()
            }
            .tabItem {
                Label("Favorites", systemImage: "heart.circle")
            }
            NavigationStack(path: $router.searchPath) {
                SearchContainer()
            }
            .tabItem {
                Label("Search", systemImage: "magnifyingglass.circle")
            }
        }
    }
}
M Alamin
  • 307
  • 2
  • 10

0 Answers0