I have date picker in my view like this:
struct BookTimeView: View {
@ObservedObject var viewModel: BookTimeViewModel
@State var startTime = Date.now
var body: some View {
DatePicker(“pick time”, selection: $startTime, displayedComponents: .hourAndMinute)
}
}
initial startTime depends on data from backend.
So in view model I have API call that loads data from backend.
In success callback I want to change startTime.
Question: How can I change startTime from my ViewModel ?
P.S.: Is that possible to bind DatePicker with ViewModel directly ?
I tried to put @State var startTime = Date.now
in view model.
But it doesn't work, and I see warning in Xcode:
Accessing State's value outside of being installed on a View. This will result in a constant Binding of the initial value and will not update.