-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mandatory pops to the root view when modifying observed object in the stacked view #38
Comments
OK, seems that I found a little clue, the ContentView struct was being refactored once observed model are updated. The temporary solution is to declare the navigation stack within Model: import SwiftUI
import NavigationStack
class Model : ObservableObject {
@Published var counter1 : Int = 0
@Published var counter2 : Int = 0
@Published var stack = NavigationStack()
}
struct SecondView: View {
@ObservedObject var model : Model
var body: some View {
VStack() {
Text("2st Page")
Button(action: {
model.counter2 += 1
print(model.counter2)
}, label: {
Text("Press to increase (\(model.counter2))")
})
PopView(label: {
Text("Back")
})
}
}
}
struct ContentView: View {
@ObservedObject var model : Model = Model()
var body: some View {
NavigationStackView(navigationStack: model.stack) {
VStack() {
Text("1st Page")
Button(action: {
model.counter1 += 1
print(model.counter1)
}, label: {
Text("Press to increase (\(model.counter1))")
})
PushView(destination: SecondView(model : model), label: {
Text("Go to 2nd view")
})
}
}
}
} The reason why refactoring occurs is still not sure though. |
@StateObject would probably help you here, replace @ObservedObject var model : Model = Model() with @StateObject var model : Model = Model() |
Hi @xarple what you are experiencing is something related to how SwiftUI works. When you change a
and your
The solution you found is also valid, but it strictly depends on your needs. If it's ok for you to inject the |
Unfortunately, in this case, |
I was commenting on his refactored version with having the NavigationStack inside the model. If his ContentView gets recreated then his model will get reinitialised, including the stack, and he will have the same problem. I know this because my use of NavigationStackView occurs a few screens deep into my App. So I agree with the views will get redrawn with ObservableObject and StateObject, however the model itself will not get recreated using @StateObject. I personally use @State in my App for the NavigationStack so I can pass it through bindings to its subviews, but it is the same effect as @StateObject. Thanks for creating this Repo, my App wouldn't have been the same without it! |
@brett-eugenelabs Yes, you're right. I thought you were commenting on the first version. Thank you so much for your words 🙏 🙏 |
Hi, this one of the great library I ever found. However when trying to modifying an observed object in the second view (stack view), then it pop to the root view immediately, here is the minimum code to reproduce the issue, please have a look:
When press the "Press to increase" button in the second view, the app pop to the root view immediately
Runs on Xcode 12.2 & iOS simulator iPhone 12 Pro, iOS 14.2
The text was updated successfully, but these errors were encountered: