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

2
  • What is the full content of the path array at the point you want to move from PathD to PathB? Commented Oct 20, 2022 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"]. Commented Oct 20, 2022 at 15:52

2 Answers 2

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"]

Sign up to request clarification or add additional context in comments.

2 Comments

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? developer.apple.com/documentation/swiftui/… this is a reference that u may look up. there is a function called read..() which I believe can somehow be helpful.
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?
-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.