0PricingLogin
SwiftUI Academy · Lesson

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

  1. Why MVVM Fits SwiftUI
  2. Designing a ViewModel
  3. Binding Views to ViewModels
  4. Dependency Injection for ViewModels
← Back to SwiftUI Academy