ios NavigationLink back无法使用@Binding

9udxz4iz  于 2023-04-08  发布在  iOS
关注(0)|答案(2)|浏览(100)

我的应用程序支持iOS 15.0,所以,我不能使用NavigationPath。所以,我添加了NavigationView并使用NavigationLink进行导航。这是我的简单应用程序流程。
FirstView -〉SecondView -〉ThirdView -〉ForthView
我想从ForthView导航到SecondView。但是导航回来不起作用。下面是我的代码。

struct FirstView: View {
    @State private var isActive: Bool = false
    var body: some View {
        VStack(spacing: 8) {
            Label("Hello, world!", systemImage: "globe")
            NavigationLink(destination: SecondView(), isActive: $isActive) {
                Button {
                    isActive = true
                } label: {
                    Text("Second")
                }
            }
        }
        .padding()
    }
}

struct SecondView: View {
    @State private var isActive: Bool = false
    var body: some View {
        VStack(spacing: 8) {
            Text("Hello, World!")
            NavigationLink(destination: ThirdView(shouldGoToSecond: $isActive), isActive: $isActive) {
                Button {
                    isActive = true
                } label: {
                    Text("Third")
                }
            }
        }
        .navigationTitle("Second")
    }
}

struct ThirdView: View {
    @Binding var shouldGoToSecond: Bool
    @State private var isActive: Bool = false
    var body: some View {
        VStack {
            Text("Hello, World!")
            NavigationLink(destination: ForthView(shouldGoToSecond: $shouldGoToSecond), isActive: $isActive) {
                Button {
                    isActive = true
                } label: {
                    Text("Go to Forth")
                }
            }
        }
        .navigationTitle("Third")
    }
}

struct ForthView: View {
    @Binding var shouldGoToSecond: Bool
    var body: some View {
        VStack {
            Text("Hello, World")
            Button {
                shouldGoToSecond = false
            } label: {
                Text("Go to Second")
            }
        }
    }
}

我正在使用@State变量来激活导航链接,并通过@Binding将其传递到下一个视图,但它不起作用。

bqucvtff

bqucvtff1#

当您有多个导航时,您可以通过使用NavigationLinkisDetailLink属性来实现它。

struct FirstView: View {
    @State private var isActive: Bool = false
    var body: some View {
        
        NavigationView {
            VStack(spacing: 8) {
                Label("Hello, world!", systemImage: "globe")
                NavigationLink(destination: SecondView1(), isActive: $isActive) {
                    Button {
                        isActive = true
                    } label: {
                        Text("Second")
                    }
                }.isDetailLink(false) //<---
            }
            .padding()
            
        }
    }
}

struct SecondView: View {
    @State private var isActive: Bool = false
    var body: some View {
        VStack(spacing: 8) {
            Text("Hello, World!")
            NavigationLink(destination: ThirdView(shouldGoToSecond: $isActive), isActive: $isActive) {
                Button {
                    isActive = true
                } label: {
                    Text("Third")
                }
            }
        }
        .navigationTitle("Second")
        
    }
}

struct ThirdView: View {
    @Binding var shouldGoToSecond: Bool
    @State private var isActive: Bool = false
    var body: some View {
        VStack {
            Text("Hello, World!")
            NavigationLink(destination: ForthView(shouldGoToSecond: $shouldGoToSecond), isActive: $isActive) {
                Button {
                    isActive = true
                } label: {
                    Text("Go to Forth")
                }
            }
        }
        .navigationTitle("Third")
    }
}

struct ForthView: View {
    @Binding var shouldGoToSecond: Bool
    var body: some View {
        VStack {
            Text("Hello, World")
            Button {
                shouldGoToSecond = false
            } label: {
                Text("Go to Second")
            }
        }
    }
}
enyaitl3

enyaitl32#

你应该用
@Environment(\.presentationMode) var presentaionMode

相关问题