A toggle switch is a UI control that lets users turn a setting on or off, similar to a light switch in real life.
Click to reveal answer
beginner
Which SwiftUI view is used to create a toggle switch?
The Toggle view is used to create a toggle switch in SwiftUI.
Click to reveal answer
intermediate
How do you bind a toggle switch to a state variable in SwiftUI?
You bind a toggle to a @State variable using the isOn parameter, so the toggle updates the variable automatically.
Click to reveal answer
beginner
What is the default appearance of a toggle switch on iOS?
The toggle switch appears as a rounded rectangle slider that moves left or right to indicate off or on states, with a green color when on.
Click to reveal answer
intermediate
Why is accessibility important for toggle switches?
Accessibility ensures that all users, including those with disabilities, can understand and operate the toggle switch using screen readers and keyboard navigation.
Click to reveal answer
Which SwiftUI property wrapper is commonly used to store the toggle state?
A@Binding
B@ObservedObject
C@State
D@EnvironmentObject
✗ Incorrect
The @State property wrapper stores local state, such as the on/off value of a toggle.
What does the toggle switch represent in an app?
AA control to turn a setting on or off
BA control to select multiple options
CA slider to adjust volume
DA button to submit data
✗ Incorrect
A toggle switch lets users turn a setting on or off, like a light switch.
How do you create a toggle switch with a label in SwiftUI?
AText("Label")
BButton("Label", action: toggleAction)
CSlider(value: $stateVariable)
DToggle("Label", isOn: $stateVariable)
✗ Incorrect
The Toggle view takes a label string and a binding to a state variable.
What color does the toggle switch show when it is ON by default?
AGreen
BBlue
CRed
DGray
✗ Incorrect
The toggle switch shows green color when it is ON by default on iOS.
Which accessibility feature helps users understand toggle switches?
AInvert Colors
BVoiceOver
CZoom
DAssistiveTouch
✗ Incorrect
VoiceOver reads out the toggle state and label to help visually impaired users.
Explain how to create and use a toggle switch in SwiftUI.
Think about how you connect the toggle to a variable that changes when the user taps it.
You got /5 concepts.
Describe why accessibility is important for toggle switches and how you can support it.
Consider users who cannot see or use touch easily.
You got /5 concepts.
Practice
(1/5)
1. What does a UISwitch control in an iOS app?
easy
A. It lets users toggle between on and off states.
B. It displays a list of options to select from.
C. It shows a progress bar for tasks.
D. It opens a new screen when tapped.
Solution
Step 1: Understand the purpose of UISwitch
A UISwitch is designed to let users turn a setting on or off with a simple tap.
Step 2: Compare with other UI controls
Unlike lists or progress bars, UISwitch specifically toggles between two states: on and off.
Final Answer:
It lets users toggle between on and off states. -> Option A
Quick Check:
UISwitch = toggle on/off [OK]
Hint: UISwitch always toggles between two states [OK]
Common Mistakes:
Confusing UISwitch with buttons that open new screens
Thinking UISwitch shows progress or lists
Assuming UISwitch can select multiple options
2. Which of the following is the correct way to create a UISwitch in Swift?
easy
A. let toggle = UISwitch.new()
B. let toggle = Switch()
C. let toggle = UISwitch()
D. let toggle = UISwitch.create()
Solution
Step 1: Recall UISwitch initialization syntax
In Swift, UI elements like UISwitch are created by calling their initializer with parentheses.
Step 2: Check each option
Only UISwitch() is the correct initializer. The others use invalid methods or class names.
Final Answer:
let toggle = UISwitch() -> Option C
Quick Check:
Initialize UISwitch with UISwitch() [OK]
Hint: Use parentheses to create UISwitch instance [OK]
Common Mistakes:
Using incorrect class name like Switch
Trying to call .new() or .create() methods
Omitting parentheses when creating UISwitch
3. What will be printed when this Swift code runs?
let toggle = UISwitch()
toggle.isOn = true
print(toggle.isOn)
medium
A. Compilation error
B. false
C. nil
D. true
Solution
Step 1: Understand the isOn property
The isOn property of UISwitch indicates if the switch is on (true) or off (false).
Step 2: Analyze the code
The code sets toggle.isOn = true and then prints toggle.isOn, so it will print true.
Final Answer:
true -> Option D
Quick Check:
Setting isOn true prints true [OK]
Hint: Print isOn after setting it to see true/false [OK]
Common Mistakes:
Assuming default is false without setting
Confusing isOn with other properties
Expecting nil or error from print
4. Identify the error in this Swift code that tries to detect switch changes:
The action method must accept the sender UISwitch to get its current state.
Step 2: Verify label text update logic
@objc func switchChanged(_ sender: UISwitch) {
label.text = sender.isOn ? "ON" : "OFF"
} uses a ternary operator to set label text to "ON" if switch is on, else "OFF", which is correct.