اختيار الخيارات باستخدام Picker
وفّر خيارات باستخدام أدوات اختيار من القوائم والمقاطع
اختيار الخيارات باستخدام Picker درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في SwiftUI Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Meet the Picker
A Picker lets users choose one option from several. It is the go-to for selecting a category, color, or plan. 📋
Picker Tracks a Selection
A Picker stores the selected value in state. As the user chooses, that bound property updates to match.
@State private var fruit = "Apple"Bind the Selection
Pass a label and the selection binding so the Picker knows which property to read and update.
Picker("Fruit", selection: $fruit) {
// options go here
}List the Options
Inside the closure, add a view per choice and tag each with its value using tag so selection can match it.
Text("Apple").tag("Apple")
Text("Pear").tag("Pear")Loop with ForEach
For many options, generate them with ForEach over an array instead of writing each row by hand.
ForEach(fruits, id: \.self) { name in
Text(name).tag(name)
}Match Selection Types
The selection type must match the tag type. If you select a String, every tag must also be a String.
The Menu Style
Apply pickerStyle to change the look. The menu style shows the choice and pops options on tap.
Picker("Fruit", selection: $fruit) { }
.pickerStyle(.menu)The Segmented Style
For a few short options, the segmented style lays them out as a row of side-by-side buttons.
.pickerStyle(.segmented)The Wheel Style
The wheel style shows a spinning drum, classic for dates or long lists where scrolling feels natural.
.pickerStyle(.wheel)React to the Choice
Read the bound value anywhere to react to the user's pick, like showing the selected fruit in a Text.
Text("You picked \(fruit)")The Full View
Here is a complete Picker: a selection binding, options from ForEach, and the segmented style applied.
struct ChooseView: View {
@State private var fruit = "Apple"
let fruits = ["Apple", "Pear"]
var body: some View {
Picker("Fruit", selection: $fruit) {
ForEach(fruits, id: \.self) { Text($0).tag($0) }
}
}
}Quick Check
Why must each Picker option be tagged?
Recap
A Picker binds to a selection, tags each option, and changes look with menu, segmented, or wheel styles. 📋
تعلم Swift مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 30
- الدروس
- 120
الأسئلة الشائعة
هل درس «اختيار الخيارات باستخدام Picker» مجاني؟
نعم — نص درس «اختيار الخيارات باستخدام Picker» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
ماذا ستتعلم في «اختيار الخيارات باستخدام Picker»؟
وفّر خيارات باستخدام أدوات اختيار من القوائم والمقاطع تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟
لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «اختيار الخيارات باستخدام Picker»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟
نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- التشغيل والإيقاف باستخدام Toggle
- اختيار القيم باستخدام Slider
- زيادة القيم باستخدام Stepper
- اختيار الخيارات باستخدام Picker