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

SOLVED: Cannot Navigate to another View

Forums > SwiftUI

I have been trying a simple navigation from toolbar, however the navigation button just does not work, can any one suggest where the issue might be

struct MyAdsPageView: View {
    var documents = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    @State  var gotoAdd = false
    @ObservedObject var dataAd: DataController
    @Environment(\.managedObjectContext) var managedObjectContext
    @State var gotoEdit = false

    var body: some View {

        NavigationView {
            List {

                ForEach(dataAd.carSheet, id:\.self) { path in
                    NavigationLink(destination: DetailView(cardata: path)) {
                        if let data = try? Data(contentsOf: documents.appendingPathComponent(path.imageNameTemp)), let loaded = UIImage(data: data) {

                            HStack {

                                Text(path.makeYearTemp)
                                    .padding()
                            }
                            .swipeActions {
                                Button {
                                    gotoEdit = true

                                } label: {
                                    Label("Edit", systemImage: "pencil")
                                }
                                .tint(.red)

                            }

                            NavigationLink("", isActive: $gotoEdit, destination: {
                                EditViewData(cardata: path, carid: path.caridTemp, carBrand: path.brandNameTemp)
                            })

                            NavigationLink("", isActive: $gotoAdd) {
                                AddView(dataController: DataController.shared)
                            }

                        }

                    }

                }

            }

            .navigationTitle("Car List")

            .toolbar {
                ToolbarItem {
                    Button("Add Car") {  <--------- this link is just not navigating, i tried NavigationStack as well
                        gotoAdd = true
                    }
                }
            }
        }

    }

}

1      

@Bnerd  

Maybe missing the ,destination?

NavigationLink("", isActive: $gotoAdd, destination:  {
                                AddView(dataController: DataController.shared)
                            })

1      

@Bnerd, thanks, i tried that but i can see the button is tapped and the control goes inside and toggles the value but no navigation, i am thinking some issue with multiple navigations links, but in so far i know , this is first time its not working with multiple nav links

1      

This link is inside your ForEach

NavigationLink("", isActive: $gotoAdd) {
    AddView(dataController: DataController.shared)
}

So when you set gotoAdd to true using the Button, which of the views in the ForEach is it meant to use for the navigation destination? It just doesn't know which one to use, and you have not specified it, so it does nothing.

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.