I also had that problem, this is the solution: add @Bindable var viewModel = viewModel in your view’s body, even though I don’t think it is elegant.
@Observable
final class View1Model {
var text: String = ""
}
struct View1: View {
@State var viewModel = View1Model()
var body: some View {
View2()
.environment(viewModel)
}
}
struct View2: View {
@Environment(View1Model.self) var viewModel
var body: some View {
// use @Bindable to get the binding
@Bindable var viewModel = viewModel
TextField("Text", text: $viewModel.text)
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: