UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Navigation View

Forums > SwiftUI

Hi there,

I have my fetched objects listed in a vstack. However, I cant click an individual one, the whole list becomes a navigation link. I get an error messege in xcode: SwiftUI encountered an issue when pushing a NavigationLink. Please file a bug.

 var body: some View {
        NavigationView{
            Form {
                Section{
                    NavigationLink{
                        NewProject()
                    } label: {
                        Text("Create a new project")
                    }

                }

                    List {
                        ForEach(project) { project in

                            NavigationLink {
                                ProjectDetailView(project: project)
                            } label: {
                                HStack{
                                    LazyVStack(alignment: .leading){
                                        Text(project.projectName ?? "Unknown Project Name")
                                        Text("Pilot: \(project.pic ?? "")")
                                        Text("Crew Members: \(project.crewMembers ?? "")")
                                        //Text(project.flightDate!)
                                    }
                                }
                            }

                        }
                        .onDelete(perform: deleteProjects)

2      

What is project that you are using in ForEach(project)

Is this a collection of all of your projects? Or is it a single project?

I don't know if that is were your problem is, but it is a bit confusing looking at

ForEach(project) { project in
    // more code here
}

Because that means that 2 variables named project that represent different things are availible within the scope of the ForEach block.

2      

I was able to get the code (or something very similar) working as I would expect it to by creating my own data model to go along with it. So there doesn't seem to be a problem with a way that you have set up the List itself from what I can tell. But I don't know what the rest of your code looks like, so it's difficult to say where the problem is coming from.

If you'd like, you can copy this code into a new project, and you will see that it runs, and treats each Project as a separate item in the list when you use the buttons to create new projects.

import SwiftUI

struct ContentView: View {
    @StateObject var projects = Projects()

    var body: some View {
        NavigationView{
            Form {
                Section {
                    NavigationLink {
                        NewProjectView(projects: projects)
                    } label: {
                        Text("Create a new project")
                    }
                }

                List {
                    ForEach(projects.all) { project in
                        NavigationLink {
                            ProjectDetailView(project: project)
                        } label: {
                            HStack {
                                LazyVStack(alignment: .leading) {
                                    Text(project.name)
                                    Text("Pilot: \(project.pic)")
                                    Text("Crew Members: \(project.crewMembers)")
                                }
                            }
                        }
                    }
                    .onDelete(perform: deleteProjects)
                }
            }
        }
    }

    func deleteProjects(offsets: IndexSet) {
        self.projects.all.remove(atOffsets: offsets)
    }
}

struct Project: Identifiable {
    var id = UUID()
    let name: String
    let pic: String
    let crewMembers: String
}

class Projects: ObservableObject {
    @Published var all: [Project]

    init(projects: [Project] = []) {
        self.all = projects
    }
}

struct ProjectDetailView: View {
    let project: Project

    var body: some View {
        Text(project.name)
    }
}

struct NewProjectView: View {
    @ObservedObject var projects: Projects

    var body: some View {
        Button("Add Project") {
            projects.all.append(Project(name: "Project1", pic: "None", crewMembers: "Nobody"))
        }
    }
}

2      

Awesome, thank you very much for the help. I did a goofy thing by attaching my searchable to the wrong view! Its fixed now! Thank you fellow 'Fly'er :D

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.