ربط الواجهات بـ ViewModels
حرّك الواجهة باستخدام خصائص ViewModel
ربط الواجهات بـ ViewModels درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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.
الأسئلة الشائعة
هل درس «ربط الواجهات بـ ViewModels» مجاني؟
نعم — نص درس «ربط الواجهات بـ ViewModels» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
ماذا ستتعلم في «ربط الواجهات بـ ViewModels»؟
حرّك الواجهة باستخدام خصائص ViewModel تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟
لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «ربط الواجهات بـ ViewModels»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟
نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- لماذا يناسب MVVM لغة SwiftUI
- تصميم ViewModel
- ربط الواجهات بـ ViewModels
- حقن التبعيات في ViewModels