1

I want to go from View PathD to PathB in the NavigationStack withOut creating a new Object of PathB and also not removing the view instance of PathC that is in the Navigation Stack Hierarchy.

Details:

@State var path: [String] = []

// or this can also be written
//@State var path: NavigationPath = NavigationPath()

var body: some View {
        NavigationStack(path: $path) {
            ZStack{         
                Text("SomeThing")
            }
            .navigationDestination(for: String.self, destination: { path in 
                switch path {      
                case "pathA" :
                   PathA().navigationBarBackButtonHidden()
                case "pathB":
                    PathB().navigationBarBackButtonHidden()
                case "pathC":
                    PathC().navigationBarBackButtonHidden()
                default:
                    PathD().navigationBarBackButtonHidden()
                }
            })

here what I tried is matching the reference name when navigation is done in some view

 path.append("pathA")

now consider I am in view PathD(). And I want to navigate back to PathB. one option is to slide around but I am disabling the navigation back button.

so what I do is

from PathD

path.append("pathB")

This will create a new PathB() view instead of returning to the one I have.

Now my requirement is to go back to the PathB() that I created and not a new object.

feel free to comment if my explanation is not sufficient

  • What is the full content of the `path` array at the point you want to move from `PathD` to `PathB`? – Scott Thompson Oct 20 '22 at 13:45
  • @ScottThompson, initially its an empty array, but at the time of PathD, it should contain, lets say, path: [String] = ["pathA","pathB","pathC","pathD"]. – Robin George Oct 20 '22 at 15:52

2 Answers2

0

I would do something like:

var newPath = Array(path.prefix(while: {$0 != "pathB"}))
newPath.append("pathB")
path = newPath

given path = ["pathA","pathB","pathC","pathD"]

This should set path to ["pathA", "pathB"]

Scott Thompson
  • 22,629
  • 4
  • 32
  • 34
  • this will destroy the path information in "pathC" and "pathD". right. i have tried similar before with functions.contain() and filter(). but is there a way to save the instances that are created so I can pick the one I want if already initiated? https://developer.apple.com/documentation/swiftui/navigationpath?changes=_5 this is a reference that u may look up. there is a function called read..() which I believe can somehow be helpful. – Robin George Oct 21 '22 at 06:43
  • The Navigation Stack is a tool for presenting a set of pages arranged in a hierarchy. The user pushes pages onto a stack, and then can navigate back up that stack. It's not clear to me what you are trying to accomplish. I don't understand what you mean by "save the instances that are created so I can pick the one I want if already initiated". Save the instances of what? – Scott Thompson Oct 21 '22 at 21:22
-1

In iOS < 16.0 you can use PresentationMode to dismiss the current view and return to previous, i.e. "navigate back":

// Auto-injected environment variable (no need to manually .environment it)
@Environment(\.presentationMode) var mode: Binding<PresentationMode>

func navigateBack() { mode.wrappedValue.dismiss() }

For iOS 16 onwards, this has been replaced by isPresented and dismiss. The latter can be used for an equivalent "navigate back".

private struct SheetContents: View {
    @Environment(\.dismiss) private var dismiss

    var body: some View {
        Button("Go to Previous View") {
            dismiss()
        }
    }
}

I've written some helper functions for the new Navigation system here, which you may find useful.

kwiknik
  • 570
  • 3
  • 7