-
-
Save anupamchugh/eba92fc51dbc5e155adfdc3770d31d99 to your computer and use it in GitHub Desktop.
Hello ,
I just tried this example , but I got there is a crash message
Thread 1: Fatal error: No ObservableObject of type Router found. A View.environmentObject(_:) for Router may be missing as an ancestor of this view.
@AmrAbedal same problem. Did you manage to solved it?
@Piggyback13 Yah , it solved , change line 9 to be
@StateObject var router: Router = Router()
getting this runtime warning
Publishing changes from within view updates is not allowed, this will cause undefined behavior.
from my search , some folks , say that this warning is an Xcode bug .
@AmrAbedal thank you man
I had a code snippet as below where I modified the navigationDestination with isPresented
argument to control when will the view change, but at the end of the SecondView
, the reset button didn't bring the navigationStack to the root view with ContentView
, it just do nothing when pressing the reset button.
I have tried wrapping the body within the NavigationStack(path: $router.path), but still not working.
import SwiftUI
final class Router: ObservableObject {
@Published var path = NavigationPath()
}
struct ContentView: View {
@StateObject var router: Router = Router()
@State private var showFirstView: Bool = false
var body: some View {
NavigationStack(path: $router.path) {
Button("Start") {
showFirstView = true
}
.navigationDestination(isPresented: $showFirstView) {
FirstView()
}
.navigationTitle("Home")
}
.environmentObject(router)
}
}
struct FirstView: View {
@EnvironmentObject var router: Router
@State private var showSecondView: Bool = false
var body: some View {
Button("Go second") {
showSecondView = true
}
.navigationDestination(isPresented: $showSecondView){
SecondView()
}
}
}
struct SecondView: View {
@EnvironmentObject var router: Router
var body: some View {
Button("reset"){
router.path = .init()
}
}
}
Appreciate for the help. Thanks !!
getting this runtime warning
on line https://gist.github.com/anupamchugh/eba92fc51dbc5e155adfdc3770d31d99#file-swiftui-new-navigationapis-swift-L43