Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How is viewId determined when PopView is used? #25

Closed
djrobby opened this issue Jul 7, 2020 · 2 comments
Closed

How is viewId determined when PopView is used? #25

djrobby opened this issue Jul 7, 2020 · 2 comments

Comments

@djrobby
Copy link

djrobby commented Jul 7, 2020

I'm using your NavigationStack in combination with SwiftUI's default Tabbed app, with each tab having its own NavigationView. I have wrapped the entire app's ContentView into NavigationStackView so that I can display **PushView()**s in fullscreen mode, including over the Tab bar. My question is where do I specify/define the viewId for the nested view that I would like to PopView() back to? Documentation provides a vague example of how to supply destinationId of the ChildView() when PushView() is used but I have a hard time understanding how the passed on childViewId helps in popping back to the parent or maybe a view nested 2 levels inside NavigationView? Can you please clarify or provide a slightly complex example? Thank you again!

@matteopuc
Copy link
Owner

Hi @djrobby. When you push a view inside a NavigationStackView you can always specify an id for the pushed view. In this case you are setting a sort of "label/tag" to come back to. So, when you do something like:

PushView(destination: ChildView(), destinationId: "childViewId") {
    Text("PUSH")
}

You'll always be able to do:

PopView(destination: .view(withId: "childViewId")) {
    Text("POP to ChildView")
}

For example:

import SwiftUI
import NavigationStack

struct ContentView: View {
    var body: some View {
        NavigationStackView {
            ZStack {
                Color.orange.edgesIgnoringSafeArea(.all)
                PushView(destination: ChildView(), destinationId: "childViewId") {
                    Text("PUSH to child")
                }
            }
        }
    }
}

struct ChildView: View {
    var body: some View {
        ZStack {
            Color.green.edgesIgnoringSafeArea(.all)
            PushView(destination: GrandChildView()) {
                Text("PUSH to grand child")
            }
        }
    }
}

struct GrandChildView: View {
    var body: some View {
        ZStack {
            Color.yellow.edgesIgnoringSafeArea(.all)
            PushView(destination: GrandGrandChildView()) {
                Text("PUSH to grand grand child")
            }
        }
    }
}

struct GrandGrandChildView: View {
    var body: some View {
        ZStack {
            Color.purple.edgesIgnoringSafeArea(.all)
            PopView(destination: .view(withId: "childViewId")) {
                Text("POP to child exploiting the prevously defined ID")
            }
        }
    }
}

The result is:

Sep-25-2020 17-31-18

@djrobby
Copy link
Author

djrobby commented Oct 25, 2020

Got it! Thank you 🙏

@djrobby djrobby closed this as completed Oct 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants