swiftui

Can I call a view from within Navigation Link?


I have tried this code:

NavigationLink(NavLink(mission: mission), value: mission)

but I get an error saying the compiler is unable to type check the expression in reasonable time. However when I do this it works:

NavigationLink {} label: {NavLink(mission: mission)}

What is the problem? I want to use the first iteration with navigationDestination as part of my learning which is why I haven't just abandoned it.


Solution

  • I assume you want to use value-based navigation, and use a custom view as a label for the navigation link.

    What you wrote in the first code snippet does not match any of the initialisers that NavigationLink has. The correct initialiser is init(value:label:):

    NavigationLink(value: mission) { NavLink(mission: mission) }