HomeiOS Developmentios - SwiftUI NavigationStack - Why Does View State Persist When Navigating...

ios – SwiftUI NavigationStack – Why Does View State Persist When Navigating Again and Forth?


I’ve a navigation hierarchy structured like this:

Root View -> View A -> View B -> View C -> View D
RootView() .navigationDestination(
                for: DashboardDestinations.self,
                vacation spot: { display in
                    change display {
                    case .ScreenA:
                        ScreenA()
                    case .ScreenB:
                        ScreenB()
                })
        }
class DashboardRouter: ObservableObject {
   var path = NavigationPath()

  static let shared: DashboardRouter = DashboardRouter()

  func popToRoot() {
    path = NavigationPath()
  }

  func popToView B() {
    path = NavigationPath([DashboardDestinations.ViewA, DashboardDestinations.ViewB])
  }
}

In my DashboardRouter class, I handle navigation utilizing a NavigationPath.

After I’m in View D and set off a button to return to View B by resetting the navigation path:

DashboardRouter.shared.popToViewB()

after which navigate ahead once more to View C, I discover that View C’s state is preserved from the earlier time it was proven, as a substitute of being reset.

Why does the state of View C persist after popping again to View B and navigating ahead once more? How can I be sure View C resets its state once I navigate to it once more?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments