2

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()
    }
}
3
  • You need to have navigationLink inside NavigationView. I tested by adding inside NavigationView and it’s working, else it’s disabled as you mentioned. Commented Jun 2, 2021 at 20:40
  • If I add a NavigationView it is working, you're right. But since when I have to add this? In my other code example is no NavigationView and it is working nevertheless Commented Jun 2, 2021 at 20:53
  • What does the parent view of RechtView look like? If it contains a NavigationView then RechtView is within a NavigationView and your links will work. If you trace back up the view hierarchy, I'm sure you will find a NavigationView somewhere and that'll be the difference. Commented Jun 2, 2021 at 22:06

3 Answers 3

5

You have to use NavigationLinks inside NavigationView{}. Without it NavigationLink wont work. 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 {}

Sign up to request clarification or add additional context in comments.

2 Comments

You're right, thank you! It solved my problem and you're right, in the second example is a NavigationView in previous view. It was my bad yesterday.
hilariously i made this mistake today
2

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

1 Comment

Thanks a lot! It works and you're right, in my other example there is a NavigationView in hierarchy above.
0

this code seems to show no error but does not work. when i press the text, i cannot change views. i have tried everything, copied code from the swift documentation, coped code from different forums, but it does not work at all !

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.