SwiftUI 中的 NavigationLink 不再工作

Pas*_*ram 2 ios swift swiftui swiftui-navigationlink

我\xc2\xb4m 创建一个应用程序并NavigationLink在 Swift/SwiftUI 中使用,但它不再工作了。我现在不知道从什么时候开始,但两三周前,一切都工作正常。它NavigationLinks已经在代码中存在了更长的时间,工作正常。但今天我用了新的,但它们不起作用。它会在模拟器和真实设备上查看它们是否被禁用或其他情况。它们是灰色的,您无法单击它们,或者即使单击它们,也不会发生任何情况。有什么解决办法吗?

\n
import SwiftUI\n\nstruct MedikamenteView: View {\n    var body: some View {\n        Form {\n            NavigationLink(\n                destination: ASSView(),\n                label: {\n                    Text("ASS")\n                })\n            NavigationLink(\n                destination: AdrenalinView(),\n                label: {\n                    Text("Adrenalin")\n                })\n        }\n    }\n}\n\nstruct MedikamenteView_Previews: PreviewProvider {\n    static var previews: some View {\n        MedikamenteView()\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

例如,这个工作正常:

\n
import SwiftUI\n\nstruct RechtView: View {\n    var body: some View {\n        Form {\n           NavigationLink(\n            destination: ParagraphenView(),\n            label: {\n                Text("Paragraphen")\n            })\n            NavigationLink(\n                destination: AufklaerungEinwilligungView(),\n                label: {\n                    Text("Die Aufkl\xc3\xa4rung mit nachfolgender Einwilligung")\n                })\n            NavigationLink(\n                destination: NotSanGView(),\n                label: {\n                    Text("Wichtiges aus dem NotSanG")\n                })\n        }\n        .navigationTitle("Recht")\n    }\n}\n\nstruct RechtView_Previews: PreviewProvider {\n    static var previews: some View {\n        RechtView()\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

小智 5

您必须在内部使用NavigationLinksNavigationView{}。没有它,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()
    }
}
Run Code Online (Sandbox Code Playgroud)

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