ios SwiftUI中的NavigationLink不再工作

7gcisfzg  于 2023-07-01  发布在  iOS
关注(0)|答案(3)|浏览(130)

我正在创建一个应用程序,并在Swift/SwiftUI中使用NavigationLink,但它不再工作了。我不知道从什么时候开始的,但两三周前,一切都很好。NavigationLinks已经在代码中运行了很长时间,工作正常。但是今天我用了新的,它们不起作用。它看起来在模拟器和一个真实的的设备上,如果他们被禁用或什么。它们是灰色的,你不能点击它们,或者如果你点击了它们,什么也没有发生。有什么解决办法吗?

import SwiftUI

struct MedikamenteView: View {
    var body: some View {
        Form {
            NavigationLink(
                destination: ASSView(),
                label: {
                    Text("ASS")
                })
            NavigationLink(
                destination: AdrenalinView(),
                label: {
                    Text("Adrenalin")
                })
        }
    }
}

struct MedikamenteView_Previews: PreviewProvider {
    static var previews: some View {
        MedikamenteView()
    }
}

例如,这一个工作得很好:

import SwiftUI

struct RechtView: View {
    var body: some View {
        Form {
           NavigationLink(
            destination: ParagraphenView(),
            label: {
                Text("Paragraphen")
            })
            NavigationLink(
                destination: AufklaerungEinwilligungView(),
                label: {
                    Text("Die Aufklärung mit nachfolgender Einwilligung")
                })
            NavigationLink(
                destination: NotSanGView(),
                label: {
                    Text("Wichtiges aus dem NotSanG")
                })
        }
        .navigationTitle("Recht")
    }
}

struct RechtView_Previews: PreviewProvider {
    static var previews: some View {
        RechtView()
    }
}
y53ybaqx

y53ybaqx1#

NavigationView{}内部需要使用NavigationLinks。没有它NavigationLink将无法工作。试试这个:

import SwiftUI

struct MedikamenteView: View {
    var body: some View {
        NavigationView {
            Form {
                NavigationLink(
                    destination: ASSView(),
                    label: {
                        Text("ASS")
                    })
                NavigationLink(
                    destination: AdrenalinView(),
                    label: {
                        Text("Adrenalin")
                    })
            }
        }
        
    }
}

struct MedikamenteView_Previews: PreviewProvider {
    static var previews: some View {
        MedikamenteView()
    }
}

您的第二个代码示例可能是从使用NavigationView {}的上一个视图加载的

n3ipq98p

n3ipq98p2#

我可以从评论中看到,你已经发现它只能在NavigationView中工作,现在想知道为什么。唯一重要的是,您的视图嵌入在NavigationView中,直接位于视图层次结构之上。例如,这段代码可以工作:

struct FirstView: View {
    var body: some View {
        NavigationView {
           NavigationLink(label: "Go to next view", destination: NextView())
        }
    }
}

struct NextView: View {
...

虽然这不会:

struct FirstView: View {
    @State var modalPresented = false
    var body: some View {
        NavigationView {
            Button("Show fullscreen cover"){
                modalPresented = true
            }
        }
            .fullScreenCover(isPresented: $modalPresented, content: SecondView())
    }
}

struct SecondView: View {
    var body: some View {
        NavigationLink(label: "Go to next view", destination: NextView())
        // Doesn't work because the view is in a fullScreenCover and therefore not a part of the NavigationView.
    }
}
avkwfej4

avkwfej43#

这段代码似乎没有显示错误,但不工作。当我按下文本时,我不能改变视图。我已经尝试了一切,从swift文档中复制代码,从不同的论坛复制代码,但它根本不起作用!

import SwiftUI

struct Content: View {
    var body: some View {
        Form {
            NavigationLink{
                LinearAlgebra()
            }label: {
                Text("hi")
            }
            
            NavigationLink(
                        destination: LinearAlgebra(),
                        label: {
                            Text("linearA")
                        })
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Content()
    }
}

this is what the output window shows, the links don't light up, pressing them does not do anything!

相关问题