简体   繁体   English

SwiftUI 中的 NavigationLink 不再工作

[英]NavigationLink in SwiftUI not working anymore

I´m creating an App and use NavigationLink in Swift/SwiftUI, but it doesn't work anymore.我正在创建一个应用程序并在 Swift/SwiftUI 中使用NavigationLink ,但它不再工作了。 I don't now since when, but 2 or 3 weeks ago, all working fine.我现在从什么时候开始就不知道了,但是 2 或 3 周前,一切正常。 The NavigationLinks which already are in the code for longer, working fine.已经在代码中更长的NavigationLinks工作正常。 But today I've used new ones, and they don´t work.但是今天我用了新的,它们不起作用。 It looks in the Simulator and on a real device, if they are disabled or something.如果它们被禁用或其他东西,它会在模拟器和真实设备上查看。 They are grey and you can't click on them or if you clicked on them, nothing happens.它们是灰色的,你不能点击它们,或者如果你点击它们,什么也不会发生。 Is there any solution?有什么解决办法吗?

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()
    }
}

And for example, this one is working fine:例如,这个工作正常:

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()
    }
}

You have to use NavigationLinks inside NavigationView{} .您必须在NavigationView{}中使用NavigationLinks Without it NavigationLink wont work.没有它NavigationLink将无法工作。 Try this:尝试这个:

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()
    }
}

Your second code sample might be loaded from previous view which has used NavigationView {}您的第二个代码示例可能是从使用NavigationView {}的先前视图加载的

I can see from the comments that you've found out it will only work in a NavigationView and are now wondering why.我可以从您发现它只能在NavigationView中工作的评论中看到,现在想知道为什么。 It only matters that your view is embedded in a NavigationView directly above it the View hierarchy.仅重要的是,您的视图嵌入在其视图层次结构正上方的NavigationView中。 For example, this code would work:例如,此代码将起作用:

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

struct NextView: View {
...

While this won't:虽然这不会:

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.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM