I'm trying to set a simple UISwitch that simply copy is a boolean value (off or on) in a Bool Variable. The problem is that when I run my app, every time I change the switch value, it crashes with the following error (in the pic). I attach you the code I'm trying to use:
import UIKit
class TypeOfDiabetesVC: UIViewController {
@IBOutlet weak var insulinDSwitch: UISwitch!
var insulinsSwitchState : Bool = true
// viewDidLoad and Memory warning funcs etc...
@IBAction func insulinDSwitch(_ sender: UISwitch) {
if (sender.isOn == true) {
insulinDSwitch.setOn(true, animated: true)
} else {
insulinDSwitch.setOn(false, animated: true)
}
insulinsSwitchState = sender.isOn
}
The app builds successfully but then when I tap on the switch to toggle it on or off, the app crash:
Your method in TypeOfDiabetesVC
View controller is named insulinDSwitch
but the storyboard is trying to call InsulinDSwitch
. The case difference is making the app crash.
Possible Solution : Remove the Action from storyboard and connect it again to the right method.
发生崩溃是因为您尚未将IBAction与情节提要板连接在一起,请再次执行此操作。
您的插座有问题,请检查UISwitch
附带的操作的插座。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.