Bindings are for SwiftUI views. Using @Binding in a view model isn't going to work. Creating a binding like Binding<Bool> in a view model isn't going to work either.
For passing data from a view model to a SwiftUI view, you are on the right track with the following code in ContentViewModel:
class ContentViewModel: ObservableObject {
@ObservedObject var dataManager = DataManager()
@Published var isOn: Bool = false
}
Have the view model conform to ObservableObject. Use @Published for any properties where you want the view to update when the property's value changes. The view that owns the view model uses the @StateObject property wrapper, like you have in your content view.
@StateObject var viewModel = ContentViewModel()
Use the @ObservedObject property wrapper to pass the view model from the content view to other views.
I am not sure why the content view model needs its own isOn property. Can't it use the data manager's isOn property?
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: