تعطيل الأزرار وتمكينها
تحكّم في إمكانية التفاعل باستخدام المعدِّل disabled
تعطيل الأزرار وتمكينها درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في SwiftUI Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Sometimes Tapping Should Wait
Not every button should always be tappable. A submit button might wait until a form is valid. SwiftUI handles this with the disabled modifier. 🚦
The disabled Modifier
Add disabled to any control and pass true or false. When true, the button stops responding to taps right away.
Button("Send") { send() }
.disabled(true)Driving It With a Condition
Instead of a fixed value, pass a condition. The button enables and disables itself as the underlying data changes.
Button("Send") { send() }
.disabled(message.isEmpty)Tied to State
Pair disabled with a state value and SwiftUI re-evaluates it on every change. The button reacts the moment your data updates.
@State private var name = ""
Button("Save") { save() }
.disabled(name.isEmpty)The Dimmed Look
A disabled button automatically appears dimmed. This visual cue tells people it is not ready yet, with no extra styling from you.
Validating a Form
A classic use is form validation: keep the action button off until every required field passes its check.
Button("Register") { register() }
.disabled(!isFormValid)Preventing Double Taps
During a slow task, disable the button so users cannot tap it again. Flip a loading flag while the work is running.
Button("Upload") { startUpload() }
.disabled(isLoading)Disabling a Whole Group
Apply disabled to a container and every control inside inherits it. One modifier can lock down an entire section at once.
VStack {
Button("A") { a() }
Button("B") { b() }
}
.disabled(isLocked)Children Cannot Re-enable
Once a parent is disabled, a child cannot turn itself back on. The most restrictive disabled value in the chain wins.
Disabled Still Communicates
A disabled button is not broken; it is honest. It signals that an action is unavailable right now, guiding users toward what to do first.
Enable by Flipping the Value
To re-enable, simply make the condition false again. Update the state it depends on and SwiftUI brings the button back to life.
Button("Confirm") { confirm() }
.disabled(!agreedToTerms)Quick Check
One last check on enabling and disabling.
Recap
Excellent! The disabled modifier locks a button based on a condition, dims it automatically, and re-enables when the value flips back. 🚦
الأسئلة الشائعة
هل درس «تعطيل الأزرار وتمكينها» مجاني؟
نعم — نص درس «تعطيل الأزرار وتمكينها» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
ماذا ستتعلم في «تعطيل الأزرار وتمكينها»؟
تحكّم في إمكانية التفاعل باستخدام المعدِّل disabled تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟
لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «تعطيل الأزرار وتمكينها»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟
نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إنشاء زر قابل للنقر
- تنسيق الأزرار
- تشغيل التعليمات البرمجية عند النقر
- تعطيل الأزرار وتمكينها