Binding Views to ViewModels
Drive the view from view model properties.
Binding Views to ViewModels is a free SwiftUI Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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)Calling Actions on Tap
Wire a button action straight to a ViewModel method. The view stays simple, just forwarding the intent to the brain.
Button("Reload") {
vm.load()
}Driving Conditional UI
Let ViewModel state choose the layout. Switch on isLoading to show a spinner or the content with no extra view logic.
if vm.isLoading {
ProgressView()
} else {
Text(vm.greeting)
}Two-Way Fields
For text input, bind a TextField to a ViewModel property with the dollar sign. Edits flow back into the ViewModel instantly.
TextField("Name", text: $vm.name)Loading When the View Appears
Use the .task modifier to call the ViewModel as the view shows up, kicking off the first data load automatically.
.task {
await vm.load()
}Lists From the ViewModel
Feed a List from a ViewModel array. When the model updates its items, SwiftUI diffs the rows and animates the change for you.
List(vm.items) { item in
Text(item.title)
}Keep the View Declarative
A well-bound view reads like a sentence: show this state, run that action. No calculations sneak into the body.
Passing the ViewModel Down
Child views can take a ViewModel as a plain property. The parent owns it; children just read and call its methods.
DetailView(vm: vm)Previews Love ViewModels
In a preview, hand the view a ViewModel filled with sample data. You can showcase loading, empty, and full states side by side. 👀
A Bound Screen at a Glance
This view owns a ViewModel, shows its greeting, and reloads on tap, a complete binding in just a few lines of SwiftUI.
Text(vm.greeting)
Button("Reload") { vm.load() }Quick Check
How should a view own a fresh ViewModel instance?
Recap: Binding Views to ViewModels
You connected a view to its ViewModel with @State, read its properties in body, and routed taps and input back to it. Next: dependency injection.
Frequently asked questions
Is the “Binding Views to ViewModels” lesson free?
Yes — the full text of “Binding Views to ViewModels” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “Binding Views to ViewModels”?
Drive the view from view model properties. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Binding Views to ViewModels” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why MVVM Fits SwiftUI
- Designing a ViewModel
- Binding Views to ViewModels
- Dependency Injection for ViewModels