I´m creating an App and use NavigationLink
in Swift/SwiftUI, but it doesn't work anymore. I don't now since when, but 2 or 3 weeks ago, all working fine. The NavigationLinks
which already are in the code for longer, working fine. 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()
}
}
I can see from the comments that you've found out it will only work in a NavigationView
and are now wondering why. It only matters that your view is embedded in a NavigationView
directly above it the View hierarchy. 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.
}
}