SwiftUI navigate to bottom of NavigationView stack

我的未来我决定 提交于 2020-01-24 19:40:50

问题


I have the following set up where a parent view holds a NavigationView which displays a series of pages, A, B and C. On page C there is a button which hides the navigation view. I want to have it so that when the navigation view is shown again, it automatically navigates to page A, however I am unsure how to do this with SwiftUI, how can this be achieved?

struct ParentView: View {
    @State var showNavigation:Bool = true
    var body: some View {
        ZStack {
            Button(action: {
                self.showNavigation = true
            }) {
                Text("Show navigation")
            }
            NavigationView {
                NavigationLink(destination: ChildA(showNavigation: $showNavigation)) {
                    Text("Go to A")
                }
            }.opacity(showNavigation ? 1.0 : 0.0)
        }
    }
}

struct ChildA: View {
    @Binding var showNavigation:Bool

    var body: some View {
        VStack {
            Text("A")
            NavigationLink(destination: ChildB(showNavigation: $showNavigation)) {
                           Text("Go to B")
                       }
        }
    }
}

struct ChildB: View {
    @Binding var showNavigation:Bool

    var body: some View {
        VStack {
            Text("B")
            NavigationLink(destination: ChildC(showNavigation: $showNavigation)) {
                           Text("Go to C")
                       }
        }
    }
}

struct ChildC: View {
    @Binding var showNavigation:Bool

    var body: some View {
        VStack {
            Text("C")
            Button(action: {
                self.showNavigation = false
            }) {
                Text("Hide Navigation")
            }
        }
    }
}


回答1:


The setting here is not complicated. One thing is for any intermediate view, you have to set .isDetailLink(false). Otherwise, they will be kept during rewinding.

                    struct ParentView: View {
                    @State var showNavigation:Bool = true
                    @State var isActive:Bool = true
                    var body: some View {
                        ZStack {
                            Button(action: {
                                self.showNavigation = true
                            }) {
                                Text("Show navigation")
                            }
                            NavigationView {
                                NavigationLink.init(destination:  ChildA(showNavigation: $showNavigation, isActive: $isActive ), isActive: $isActive){
                                     Text("Go to A")
                                }
                            }.opacity(showNavigation ? 1.0 : 0.0)
                        }
                    }
                }

                struct ChildA: View {
                    @Binding var showNavigation:Bool
                    @Binding var isActive:Bool
                      @State var isNextActive:Bool = false
                    var body: some View {
                        VStack {
                            Text("A")
                            NavigationLink(destination: ChildB(showNavigation: $showNavigation, isActive: $isNextActive), isActive: $isNextActive) {
                                           Text("Go to B")
                            }.isDetailLink(false)
                        }.onReceive(Just(isNextActive)) { isNextActive in
                            if isNextActive == false && (!self.showNavigation) {

                                                  self.isActive = false
                            }
                        }
                    }
                }

                struct ChildB: View {
                     @Binding var showNavigation:Bool
                     @Binding var isActive:Bool
                     @State var isNextActive:Bool = false
                    var body: some View {
                        VStack {
                            Text("B")
                            NavigationLink(destination: ChildC(showNavigation: $showNavigation, isActive: $isNextActive), isActive: $isNextActive) {
                                           Text("Go to C")
                            }.isDetailLink(false)
                        }.onReceive(Just(isNextActive)) { isNextActive in
                            if isNextActive == false && (!self.showNavigation) {
                                DispatchQueue.main.async {


                                    self.isActive = false}
                            }
                        }

                    }
                }

                struct ChildC: View {
                    @Binding var showNavigation:Bool
                   @Binding var isActive:Bool
                    var body: some View {
                        VStack {
                            Text("C")
                            Button(action: {
                                self.showNavigation = false
                                self.isActive = false
                            }) {
                                Text("Hide Navigation")
                            }
                        }
                    }
                }


来源:https://stackoverflow.com/questions/59200936/swiftui-navigate-to-bottom-of-navigationview-stack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!