I want to deport my @FocusState into my viewModel:
struct ContentView: View {
@ObservedObject private var viewModel = ViewModel()
var body: some View {
Form {
TextField("Text", text: $viewModel.textField)
.focused(viewModel.$hasFocus)
Button("Set Focus") {
viewModel.hasFocus = true
}
}
}
}
class ViewModel: ObservableObject {
@Published var textField: String = ""
@FocusState var hasFocus: Bool
}
But when I launch my app, i have this SwiftUI warning:
runtime: SwiftUI: Accessing FocusState's value outside of the body of a View. This will result in a constant Binding of the initial value and will not update.
And in this case, my binding is never changed.
My question is: It is possible to use FocusState in a viewModel?