I would like to manage all my Views on the NavigationPath
with an object NavigationObj
, so I can easily manage my headers on the NavigationPath
. I can't figure out how to send a View into NavigationObj
and keep NavigationObj
Hashable
so it can be added onto the NavigationPath
. Here is an example with the code to set the View on NavigationObj
commented out.
@available(iOS 16.0, *)
final class GlobalClass1: ObservableObject {
@Published public var navigationPath = NavigationPath()
}
@available(iOS 16.0, *)
struct NavigationObj: Identifiable, Hashable {
let id = UUID()
let name: String
var destination: some View{ContentView111()}
var body: some View {
//add custom headers and controls depending on destination
destination
}
var isEmpty: Bool {
name.isEmpty
}
}
@available(iOS 16.0, *)
struct ContentView111: View {
@EnvironmentObject private var globalObj: GlobalClass1
var body: some View {
NavigationStack(path: $globalObj.navigationPath) {
VStack{
// Button to navigate deeper
Button(action: {
// Is it possible to pass a View to NavigationObj to navigate to like the commented out part?
globalObj.navigationPath.append(NavigationObj(name: "New"))//{ ContentView111() })
}, label: {
Text("go deeper")
})
if globalObj.navigationPath.count > 0{
// Button to pop to root
Button(action: {
globalObj.navigationPath = NavigationPath()
}, label: {
Text("pop to root")
})
}
}
.navigationDestination(for: NavigationObj.self) { item in
item.destination
}
}
}
}