xcode 如何一次关闭多个导航视图

mwkjh3gx  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(121)

这里我有一个视图A、B和C,它们有以下定义和导航流。这里我如何通过忽略B和C从视图C直接转到A?
我使用这段代码来消除视图C和B

@Environment(\.presentationMode) var presentationMode

presentationMode.wrapedValue.dismiss(

)

我怎么能一下子解散呢?一个一个解散会让用户体验相当失望ios:13岁以上

struct A: View {
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: B()) {
                    Text("Go to B")
                }
            }
        }
    }
}

struct B: View {
    var body: some View {
        VStack {
            Text("This is view B")
            NavigationLink(destination: C()) {
                Text("Go to C")
            }
        }
    }
}

struct C: View {
    var body: some View {
        Text("This is view C")
    }
}
cfh9epnr

cfh9epnr1#

您可以使用@Binding并更改最后一个结构中的值(isStartShow)以转到开头。但是您不能使用导航栏中的标准按钮,您需要像示例中那样创建自己的按钮

struct A: View {
    @State private var isAstive = false
    
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(isActive: $isAstive) {
                    B(isActive: $isAstive)
                } label: {
                    Text("Go to B")
                }
            }
        }
    }
}

struct B: View {
    @Binding var isActive: Bool
    
    var body: some View {
        VStack {
            Text("This is view B")
            
            NavigationLink(destination: C(isStartShow: $isActive)) {
                Text("Go to C")
            }
        }
    } 
}

struct C: View {
    @Binding var isStartShow: Bool
    
    var body: some View {
        Text("This is view C")
            // your custom button:
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    Button {
                        isStartShow.toggle()
                    } label: {
                        HStack {
                            Image(systemName: "chevron.backward")
                            Text("Back to main")
                                .foregroundColor(.blue)
                        }
                    }
                }
            }
            .navigationBarBackButtonHidden(true)
    }
}

video example

相关问题