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      

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.