0
0
iOS Swiftmobile~20 mins

Toggle switch in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Toggle Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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()
  }
}
AToggle is enabled
BFeature is OFF
CFeature is ON
DNo text is displayed
Attempts:
2 left
💡 Hint
The label text changes based on the toggle's boolean state.
lifecycle
intermediate
1: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?
ARandom state each time
BOFF (false)
CUndefined, causes a crash
DON (true)
Attempts:
2 left
💡 Hint
Think about the default boolean value for UISwitch's isOn property.
📝 Syntax
advanced
2: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
  }
}
AToggle("Enable", isOn: $isEnabled)
BToggle("Enable", isOn: Binding(isEnabled))
CToggle("Enable", isOn: isEnabled)
DToggle("Enable", isOn: Binding($isEnabled))
Attempts:
2 left
💡 Hint
Use the $ prefix to pass a binding to the Toggle.
🔧 Debug
advanced
2: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?
AThe addTarget method is called after the switch is toggled
BThe switchChanged method is not marked @objc
CThe toggle's isOn property does not change automatically
DThe label is not added to the view hierarchy
Attempts:
2 left
💡 Hint
If the label is not visible, text changes won't show.
🧠 Conceptual
expert
3:00remaining
Accessibility for Toggle switches
Which practice best improves accessibility for a toggle switch in SwiftUI?
AAdd .accessibilityLabel("Enable feature") to the Toggle
BUse a Text view instead of Toggle for better screen reader support
CDisable the toggle to prevent accidental changes
DSet the toggle color to red for visibility
Attempts:
2 left
💡 Hint
Accessibility labels help screen readers describe controls.