Component - Toggle switch
A toggle switch is a simple on/off control that lets users choose between two states. It looks like a small sliding button that moves left or right to indicate off or on.
A toggle switch is a simple on/off control that lets users choose between two states. It looks like a small sliding button that moves left or right to indicate off or on.
UIViewController
└── UIView (main view)
└── UISwitchUISwitch control in an iOS app?UISwitch in Swift?UISwitch() is the correct initializer. The others use invalid methods or class names.let toggle = UISwitch() toggle.isOn = true print(toggle.isOn)
isOn property of UISwitch indicates if the switch is on (true) or off (false).toggle.isOn = true and then prints toggle.isOn, so it will print true.let toggle = UISwitch()
toggle.addTarget(self, action: #selector(switchChanged), for: .valueChanged)
@objc func switchChanged() {
print("Switch toggled")
}addTarget method requires the action method to accept the sender parameter.switchChanged() has no parameters but should accept _ sender: UISwitch.let toggle = UISwitch() let label = UILabel() // Which switchChanged method is correct?
Pass the test and this pattern moves unknown