TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: NavigationLinks to "Details" page do not work after data changes

Forums > SwiftUI

I'm running into this error, when the underlying SwiftData object changes:

*" A NavigationLink is presenting a value of type “Project” but there is no matching navigationDestination declaration visible from the location of the link. The link cannot be activated.

Note: Links search for destinations in any surrounding NavigationStack, then within the same column of a NavigationSplitView. "*

For instance, when I first fire up the simulator, clicking on any existing project correctly takes me to its details page. However, as soon as I add a new project or delete a project, none of the navigation links work any more. If I reload the simulator, they all work again! Makes me suspect it's an error with not recomputing the links when the underlying data change, but I can't figure out what I need to change, as everything looks correct. Any help would be appreciated!

ContentView:

struct ContentView: View {
    @Environment(\.modelContext) var modelContext
    @Query(sort: \Project.endDate) var projects: [Project]

    var body: some View {
        NavigationStack {
            Group {
                if projects.isEmpty {
                    ContentUnavailableView("Enter your first project.", systemImage: "clipboard")
                } else {
                    List {
                        ForEach(projects, id: \.id) { project in
                            NavigationLink(value: project) {
                                HStack {
                                    DaysTile(
                                        days: project.numWorkDaysNowUntilEnd,
                                        description: "Work Days Remaining"
                                    )
                                    .padding(.trailing, 5)

                                    VStack(alignment: .leading) {
                                        Text(project.name)
                                            .font(.title)

                                        Text("Start: \(project.startDate.formatted(date: .abbreviated, time: .omitted))")
                                            .foregroundStyle(.secondary)

                                        Text("End: \(project.endDate.formatted(date: .abbreviated, time: .omitted))")
                                            .foregroundStyle(.secondary)
                                    }
                                }
                            }
                        }
                        .onDelete(perform: deleteProjects)
                    }
                    .navigationDestination(for: Project.self) { project in
                        ProjectDetailView(project: project)
                    }
                }
            }
            .navigationTitle("Project Tracker")
            .toolbar {
                NavigationLink {
                    AddProjectView()
                } label: {
                    Label("Add New Project", systemImage: "plus")
                }
            }
        }
    }

    func deleteProjects(at offsets: IndexSet) {
        for offset in offsets {
            let project = projects[offset]
            modelContext.delete(project)
        }
    }
}

#Preview {
    ContentView()
        .modelContainer(for: Project.self, inMemory: true)
}

   

Fixed!

I found that I had a second NavigationStack on my DetailView. Removing it, so there was only one in ContentView, fixed the problem!

   

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.