-0.4 C
New York
Saturday, February 22, 2025

ios – SwiftUI NavigationPath in NavigationStack(path: $path) in NavigationSplitView doesn’t Compile


I’ve a easy NavigationSplitView app with a easy SwiftData mannequin. I am attempting to make use of NavigationPath to be able to return to the ThingListView when a number of layers deep. Each article I’ve discovered and three completely different AI coders say this code ought to work, nevertheless it doesn’t compile. Within the element: under, the road “NavigationStack(path: $router.path) {” raises the error “Can’t discover ‘$router’ in scope”.

import SwiftUI
import SwiftData
import Commentary

@most important
struct ThingNavigationPathApp: App {
    
    @State personal var router = Router()
    
    var physique: some Scene {
        WindowGroup {
            ThingListView()
                //.setting(router)
        }
        .modelContainer(for: [Thing.self])
        .setting(router)
    }
}

@Observable
class Router {
    var path = NavigationPath()
        
    func navigateToRoot() {
        path = NavigationPath()
    }
}

struct ThingListView: View {
    
    @Surroundings(.modelContext) personal var context
    @Question(kind: Factor.identify) personal var issues: [Thing]
    @Surroundings(Router.self) personal var router
    
    @State personal var selectedThing: Factor?
    @State personal var searchText = ""
    
    var physique: some View {
        NavigationSplitView {
            VStack {
                Textual content("Issues")
                
                Record(choice: $selectedThing) {
                    ForEach(issues, id: .self) { factor in
                        NavigationLink(worth: factor) {
                            ThingRowView(factor: factor)
                        }
                    }
                }
                .searchable(textual content: $searchText)
                
                Button(motion: {
                    Process {
                        //do some setup
                    }
                }, label: {
                    Textual content("Load Examples")
                })
            }
        } element: {
            NavigationStack(path: $router.path) {
                VStack {
                    if let selectedThing = selectedThing {
                        ThingDetailView(selectedThing: selectedThing)
                    } else {
                        PlaceholderView()
                    }
                }
                .navigationDestination(for: Factor.self) { factor in
                    ThingEditView(factor: factor)
                }
            }//nav
        }
    }
}

And only for completeness:

@Mannequin
remaining public class Factor {
    
    var identify: String = ""
    var remark: String = ""
    var imageData: Information?
    var depend: Int = 0
    
    init(identify: String, remark: String, imageData: Information? = nil, depend: Int) {
        self.identify = identify
        self.remark = remark
        self.imageData = imageData
        self.depend = depend
    }

    static let one = Factor(identify: "Factor One", remark: "fruit", depend: 1)
    static let two = Factor(identify: "Factor Two", remark: "vegetable", depend: 2 )
    static let three = Factor(identify: "Factor Three", remark: "protein", depend: 3)
    
}//class factor

struct ThingDetailView: View {
    @Surroundings(.modelContext) personal var context
    @Surroundings(Router.self) personal var router
    
    
    let selectedThing: Factor

    @State personal var newUIImage: UIImage? = nil
    @State personal var disableSaveButton: Bool = false
    @State personal var shouldEditPhoto: Bool = false
    
    var physique: some View {
            Record {
                Part {
                    NavigationLink(worth: selectedThing) {
                        ThingRowView(factor: selectedThing)
                    }
                } header: {
                    Textual content("Factor Title")
                }
            }
            .navigationTitle(selectedThing.identify)
    }
}

Any steering can be appreciated. Xcode 16.2, iOS 18.2

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles