SwiftUI Academy · บทเรียน

เชื่อมมุมมองกับ ViewModels

ขับเคลื่อนมุมมองด้วยคุณสมบัติของโมเดลมุมมอง

บทเรียน 3 จาก 413 ขั้นตอน

เชื่อมมุมมองกับ ViewModels เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.

เริ่มต้นได้ฟรี

เรียนรู้ Swift ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
30
บทเรียน
120

คำถามที่พบบ่อย

บทเรียน “เชื่อมมุมมองกับ ViewModels” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “เชื่อมมุมมองกับ ViewModels” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “เชื่อมมุมมองกับ ViewModels”

ขับเคลื่อนมุมมองด้วยคุณสมบัติของโมเดลมุมมอง คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “เชื่อมมุมมองกับ ViewModels” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม

ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เหตุใด MVVM จึงเหมาะกับ SwiftUI
  2. การออกแบบ ViewModel
  3. เชื่อมมุมมองกับ ViewModels
  4. การฉีดการพึ่งพาสำหรับ ViewModels
← กลับไปที่ SwiftUI Academy