Challenge - 5 Problems
Toggle Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Toggle switch state change effect
What will be the text displayed in the label after toggling the switch from OFF to ON?
iOS Swift
import SwiftUI struct ContentView: View { @State private var isOn = false var body: some View { VStack { Toggle("Enable Feature", isOn: $isOn) Text(isOn ? "Feature is ON" : "Feature is OFF") } .padding() } }
Attempts:
2 left
💡 Hint
The label text changes based on the toggle's boolean state.
✗ Incorrect
The toggle changes the isOn state from false to true. The Text view shows "Feature is ON" when isOn is true.
❓ lifecycle
intermediate1:30remaining
Toggle switch initial state in UIKit
In UIKit, if you create a UISwitch programmatically and do not set its state, what is the default state of the switch when it appears?
Attempts:
2 left
💡 Hint
Think about the default boolean value for UISwitch's isOn property.
✗ Incorrect
UISwitch defaults to OFF (isOn = false) if not explicitly set.
📝 Syntax
advanced2:00remaining
Correct SwiftUI Toggle binding syntax
Which option correctly binds a Toggle to a @State variable named isEnabled?
iOS Swift
import SwiftUI
struct MyView: View {
@State private var isEnabled = false
var body: some View {
// Toggle code here
}
}Attempts:
2 left
💡 Hint
Use the $ prefix to pass a binding to the Toggle.
✗ Incorrect
The $ prefix creates a binding to the @State variable, which Toggle requires.
🔧 Debug
advanced2:30remaining
Why does the UISwitch not update the label?
Given this UIKit code, why does the label not update when the switch is toggled?
let toggle = UISwitch()
let label = UILabel()
func setup() {
toggle.addTarget(self, action: #selector(switchChanged), for: .valueChanged)
}
@objc func switchChanged() {
label.text = toggle.isOn ? "ON" : "OFF"
}
// toggle and label are added to the view
What is the most likely reason?
Attempts:
2 left
💡 Hint
If the label is not visible, text changes won't show.
✗ Incorrect
If the label is not added to the view, updating its text has no visible effect.
🧠 Conceptual
expert3:00remaining
Accessibility for Toggle switches
Which practice best improves accessibility for a toggle switch in SwiftUI?
Attempts:
2 left
💡 Hint
Accessibility labels help screen readers describe controls.
✗ Incorrect
Adding .accessibilityLabel provides a clear description for users relying on assistive technologies.