@Environment
can't use for Binding
?
@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 {
TextField("Text", text: $viewModel.text) // Cannot find '$viewModel' in scope
}
}
struct TitleEditView: View {
@Environment(Book.self) private var book
var body: some View {
@Bindable var book = book
TextField("Title", text: $book.title)
}
}
I found this from Bindable Document https://developer.apple.com/documentation/swiftui/bindable
thanks @macrojd