SwiftUI SwiftUI Lists and Forms 2 — Questions and Answers
Question 1: Which view in SwiftUI is designed for data-entry forms?
- VStack
- List
- Form (Correct answer)
- ScrollView
Correct answer: Form
`Form` renders an adaptive grouped-list layout optimized for user input, automatically styling controls like Toggle and Picker.
Question 2: How do you bind a `TextField` to a state variable in SwiftUI?
- TextField(text: $name)
- TextField("Label", text: $name) (Correct answer)
- TextField("Label", binding: $name)
- TextField($name, label: Text(""))
Correct answer: TextField("Label", text: $name)
`TextField` takes a localized string key for its label and a `Binding<String>` for its text value.
Question 3: What does the `.keyboardType()` modifier do on a TextField?
- Changes the language of the keyboard
- Sets the type of software keyboard displayed (e.g., .emailAddress, .numberPad) (Correct answer)
- Enables autocorrect
- Controls keyboard shortcuts
Correct answer: Sets the type of software keyboard displayed (e.g., .emailAddress, .numberPad)
`.keyboardType(_:)` specifies which keyboard layout appears when the TextField becomes active.
Question 4: Which SwiftUI control presents a list of options for the user to choose from?
- Menu
- Picker (Correct answer)
- Segmented
- OptionSet
Correct answer: Picker
`Picker` renders a selection control whose style (wheel, menu, segmented, inline) adapts to context.
Question 5: How do you create an on/off toggle in SwiftUI?
- Switch(isOn: $flag)
- Toggle("Label", isOn: $flag) (Correct answer)
- Checkbox(isChecked: $flag)
- Toggle(isOn: $flag, label: { Text("Label") })
Correct answer: Toggle("Label", isOn: $flag)
`Toggle` takes a label string and an `isOn` Binding<Bool>, rendering as a system toggle switch.
Question 6: Which modifier submits a form or text field action when the user presses Return?
- .onReturn()
- .onSubmit() (Correct answer)
- .submitLabel()
- .onCommit()
Correct answer: .onSubmit()
`.onSubmit {}` executes a closure when the user submits a text field or any submittable control.
Which view in SwiftUI is designed for data-entry forms?