我的代码无效。我不知道为什么。问题是switchChanged函数的属性。如果属性为空,则代码正在运行。
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let rect = CGRectMake(130, 100, 0, 0)
let uiSwitch = UISwitch(frame: rect)
uiSwitch.setOn(true, animated: true)
uiSwitch.addTarget(self, action: "switchChanged", forControlEvents: UIControlEvents.ValueChanged)
self.view.addSubview(uiSwitch)
}
func switchChanged(uiSwitch: UISwitch) {
var message = "Turn on the switch"
if uiSwitch.on {
message = "Turn off the switch"
} else {
message = "Turn on the switch"
}
let alert = UIAlertController(title: "Information", message: message, preferredStyle: UIAlertControllerStyle.Alert)
let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
alert.addAction(action)
self.presentViewController(alert, animated: true, completion: nil)
}
错误:" libc ++ abi.dylib:以NSException类型的未捕获异常终止"
答案 0 :(得分:3)
"switchChanged"
不是正确的选择器名称,您应使用"switchChanged:"
来计算参数。 switchChanged
将是一个没有参数的方法。
此外,在Swift中,您应该使用#selector(switchChanged(_:))
代替。这将在编译期间验证选择器的存在。