Binding Views to ViewModels
Drive the view from view model properties.
Owning the ViewModel
The view that creates a ViewModel owns it. Use @State to hold the instance so it survives across re-renders of the view.
@State private var vm = ProfileViewModel()Reading State in body
Inside body, read the ViewModel properties like normal values. SwiftUI re-runs body whenever those observed properties change.
Text(vm.greeting)All lessons in this course
- Why MVVM Fits SwiftUI
- Designing a ViewModel
- Binding Views to ViewModels
- Dependency Injection for ViewModels