التشغيل والإيقاف باستخدام Toggle
اربط Toggle بإعداد منطقي
التشغيل والإيقاف باستخدام Toggle درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في SwiftUI Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Meet the Toggle
A Toggle is SwiftUI's on/off switch. It is perfect for settings like notifications, dark mode, or any yes-or-no choice. 🔘
Toggle Needs a Boolean
Every Toggle reflects a Bool value. When the switch is on the value is true; when it is off the value is false. They stay in sync.
Store It in @State
Hold the on/off value in a @State property so the view can read and update it as the user flips the switch.
@State private var isOn = falseBind with the Dollar Sign
Connect the Toggle to your state using the dollar sign. That creates a two-way binding so flips update the value automatically.
Toggle("Notifications", isOn: $isOn)Give It a Clear Label
The first argument is the label users read next to the switch. Keep it short and describe exactly what the toggle controls.
Toggle("Dark Mode", isOn: $isDark)React to the Value
Read the boolean anywhere in your body to react to the switch. Here the text changes based on whether the toggle is on.
Text(isOn ? "On" : "Off")Put It in a Form
Toggles look most at home inside a Form, where they automatically gain the familiar iOS settings-row styling.
Form {
Toggle("Wi-Fi", isOn: $wifiOn)
}A Richer Label View
Need an icon too? Use the closure form so the label can be any view, like a Label with an SF Symbol.
Toggle(isOn: $isOn) {
Label("Sound", systemImage: "speaker")
}Style the Switch
Change the look with toggleStyle. The button style turns it into a tappable button instead of the default switch.
Toggle("Star", isOn: $isOn)
.toggleStyle(.button)Tint the Track
Set the on-color of the switch with the tint modifier, so it matches your app's accent and feels branded.
Toggle("On", isOn: $isOn)
.tint(.green)The Full View
Here it all comes together: one boolean, one Toggle bound to it, and a Text that responds to every flip.
struct SettingsView: View {
@State private var isOn = false
var body: some View {
Toggle("Alerts", isOn: $isOn)
Text(isOn ? "On" : "Off")
}
}Quick Check
How do you connect a Toggle to a boolean state value?
Recap
A Toggle binds to a Bool with the dollar sign, flips it on tap, and lets you react, style, and tint the switch. 🔁
تعلم Swift مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 30
- الدروس
- 120
الأسئلة الشائعة
هل درس «التشغيل والإيقاف باستخدام Toggle» مجاني؟
نعم — نص درس «التشغيل والإيقاف باستخدام Toggle» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
ماذا ستتعلم في «التشغيل والإيقاف باستخدام Toggle»؟
اربط Toggle بإعداد منطقي تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟
لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «التشغيل والإيقاف باستخدام Toggle»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟
نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- التشغيل والإيقاف باستخدام Toggle
- اختيار القيم باستخدام Slider
- زيادة القيم باستخدام Stepper
- اختيار الخيارات باستخدام Picker