-
-
Save anupamchugh/eba92fc51dbc5e155adfdc3770d31d99 to your computer and use it in GitHub Desktop.
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 !!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Piggyback13 Yah , it solved , change line 9 to be
@StateObject var router: Router = Router()