Error While Navigating From Double Column Navigation View #51
-
Hi I am making a app and I want to implement Double Column Navigation View in Mac, when I open my inside the main screen I easily navigate using Navigation Stack and Push and Pop my views but when I tried to open link from SideBar then I tried to open any link it shows error "No ObservableObject of type NavigationStack found. A View.environmentObject(_:) for NavigationStack may be missing as an ancestor of this view." Can you please suggest me solution you help will highly appreciated. Sidebar View
` |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @HaarisIqubal. It would be great to have a minimum viable example to try and debug. If I got what you mean, I think the problem is that you are trying to use the navigation stack environment object from a completely different view hierarchy. What about having two different struct ContentView: View {
@StateObject var mainNavStack = NavigationStack()
@StateObject var secondaryNavStack = NavigationStack()
var body: some View {
NavigationView {
NavigationStackView(navigationStack: mainNavStack) {
MainView()
}
NavigationStackView(navigationStack: secondaryNavStack) {
SecondaryView()
}
}
.navigationViewStyle(DoubleColumnNavigationViewStyle())
}
}
struct MainView: View {
var body: some View {
PushView(destination: MainChild()) {
Text("PUSH TO MAIN CHILD")
}
}
}
struct MainChild: View {
var body: some View {
PopView() {
Text("POP TO MAIN VIEW")
}
}
}
struct SecondaryView: View {
var body: some View {
PushView(destination: MainChild()) {
Text("PUSH TO SECONDARY CHILD")
}
}
}
struct SecondaryChild: View {
var body: some View {
PopView() {
Text("POP TO SECONDARY VIEW")
}
}
} You can even store your navigation stack (or stacks) somewhere, inject them in your |
Beta Was this translation helpful? Give feedback.
Hi @HaarisIqubal. It would be great to have a minimum viable example to try and debug. If I got what you mean, I think the problem is that you are trying to use the navigation stack environment object from a completely different view hierarchy. What about having two different
NavigationStackView
? Something like: