Search code examples
iosswiftuiswiftui-navigationstack

NavigationStack - NavigationLink doesn't work


In the MainView, I have this below NaviationStack:

.navigationDestination(for: String.self){ selection in
    
    if(selection == "login"){
        Login()
    }else if(selection == "register"){
        CreateAccount()
    }
}

In a sub view, I have this:

NavigationLink(value: "login"){
    Image(systemName: "person.circle.fill")
        .resizable()
        .aspectRatio(contentMode: .fit)
        .frame(height: profImgSize)
        .foregroundColor(Color(UIColor(named: "IconColor")!))
}

When I tap on it, I get:

Note: Links search for destinations in any surrounding NavigationStack, then within the same column of a NavigationSplitView.
A NavigationLink is presenting a value of type “String” but there is no matching navigationDestination declaration visible from the location of the link. The link cannot be activated.

When I create a minimal example, then it works and when I would post all the code here then it would be way too much. What could be the problem here?


Solution

  • Your navigationDestination is outside of a NavigationStack. The error message tells you that when it says:

    Links search for destinations in any surrounding NavigationStack

    When looking at the documentation, you can see that navigationDestination should go inside the NavigationStack:

    NavigationStack {
        List(parks) { park in
            NavigationLink(park.name, value: park)
        }
        .navigationDestination(for: Park.self) { park in
            ParkDetails(park: park)
        }
    }