TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Pop multiple views off a navigation stack

Forums > SwiftUI

I have a tabbed view where one of the tabs takes the user through a number of "Add Item" screens, which are part of a Navigation Stack. The general workflow is as follows:

Tab - Add Item Home -> Add Item selected -> Enter item name (Screen #1) -> Next -> Enter Item details (Screen #2) -> Next -> Enter Item details (Screen #3) -> Done

When the user selects Done I would like to pop all of teh views off the navigation stack and return to the 'Add Item Home' screen. I know I can use

 @Environment(\.presentationMode) var mode: Binding<PresentationMode>

combined with

self.mode.wrappedValue.dismiss()

to dismiss one screen (the equivalent of pressing back) but how do I pop all views from Navigation stack?

4      

I too, wonder if there's a way to go about doing this without just rendering a view programmatically.

Right now, I just have an if/else in my code that wraps the whole thing...

if (self.showHomeView) {
  HomeView()
} else {
  // Some other view that is nested deeply in my app but when this page is done I want to go back to the home view
  VStack {
    Text("Hello world")
  }
}

3      

Just found this stack overflow and the first answer seems to be a good one https://stackoverflow.com/questions/57334455/swiftui-how-to-pop-to-root-view/59662275#59662275

3      

Xcode 13 beta 3 self.mode.wrappedValue.dismiss() pop to the root not single. For example login->home->detail->subdetails( Using NavigationView and NavigationLink ). if i execute code on subdetails screen. It's pop to login. Its very strange.

3      

Try NavigationViewKit https://github.com/fatbobman/NavigationViewKit

import NavigationViewKit
NavigationView {
            List(0..<10) { _ in
                NavigationLink("abc", destination: DetailView())
            }
        }
        .navigationViewManager(for: "nv1", afterBackDo: {print("back to root") })

in any view in NavigationView

@Environment(\.navigationManager) var nvmanager         

Button("back to root view") {
    nvmanager.wrappedValue.popToRoot(tag:"nv1"){
             print("other back")
           }
}

You can also call it through NotificationCenter without calling it in the view

let backToRootItem = NavigationViewManager.BackToRootItem(tag: "nv1", animated: false, action: {})
NotificationCenter.default.post(name: .NavigationViewManagerBackToRoot, object: backToRootItem)

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.