我正在创建一个类来管理复选框。
这就是我所做的:
import UIKit
class CheckboxButton: UIButton {
//let checked = ""
//let unchecked = ""
let checked = "bubu"
let unchecked = "baba"
var isChecked:Bool = false{
didSet{
if isChecked == true {
self.setTitle(checked, forState: UIControlState.Normal)
}else{
self.setTitle(unchecked, forState: UIControlState.Normal)
}
}
}
override func awakeFromNib() {
self.titleLabel?.font = UIFont(name: "FontAwesome", size: 20)
self.addTarget(self, action: "buttonClicked", forControlEvents: UIControlEvents.TouchUpInside)
self.isChecked = false
}
func buttonClicked(sender:UIButton){
if(sender == self){
if isChecked == true {
isChecked = false
}else{
isChecked = true
}
}
}
}
一切都很好,但是当我点击按钮时,应用程序崩溃时出现错误Thread 1: signal SIGABRT
有什么不对吗?
感谢您的帮助!
答案 0 :(得分:0)
由于您的操作函数采用参数func buttonClicked(sender:UIButton)
,因此目标的操作最后应始终包含:
,因此您所要做的就是用此替换添加目标行
self.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
此外,您的buttonClicked功能可以简化为:
func buttonClicked(sender:UIButton){
if(sender == self){
isChecked = !isChecked
}
}