ビューをViewModelにバインドする
ViewModelのプロパティからビューを動かします。
「ビューをViewModelにバインドする」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.
よくある質問
「ビューをViewModelにバインドする」レッスンは無料ですか?
はい。「ビューをViewModelにバインドする」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。
「ビューをViewModelにバインドする」で何を学びますか?
ViewModelのプロパティからビューを動かします。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
SwiftUI Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「ビューをViewModelにバインドする」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSwiftUI Academyレッスンでコードを書いて実行できますか?
はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- MVVMがSwiftUIに適している理由
- ViewModelを設計する
- ビューをViewModelにバインドする
- ViewModelの依存性注入