I created a checkbox button with class CheckBox
. I want to enable/disable ResultTextField
according to the value of checkbox ischecked
. So i linked a button with @IBAction func retestRequestedCheckbox_onChange()
. The proble is the func retestRequestedCheckbox_onChange()
is getting called before it updated value in CheckBox
class. How can i update the checkbox value before calling retestRequestedCheckbox_onChange()
function? Here is my code:
class Checkbox: UIButton {
let checkedImage = UIImage(named: "checked_checkBox") as UIImage?
let unCheckedImage = UIImage(named: "unchecked_checkBox") as UIImage?
var isChecked:Bool = false{
didSet
{
if isChecked == true{
self.setImage(checkedImage, forState: .Normal)
}else {
self.setImage(unCheckedImage, forState: .Normal)
}
}
}
override func awakeFromNib() {
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
}
}
}
}
@IBAction func Checkbox_onChange(){
if CheckboxButton.isChecked ?? false {
ResultTextField.enabled = true
} else {
ResultTextField = false
}
}
为什么不简单地将用于更改UI的代码放在didSet
方法中?
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.