I have two buttons modified to work as a checkbox
by a subclass ( @IBOutlet siCheckbox
and noCheckbox
) if I set a checkbox
the other is automatically unmarked the problem is that to say if I press "yes" once uncheck the "no", but if I press the "yes" again it marks out itself and marks the "no"
this is the code
import UIKit
class Paso1: UIViewController, CheckBoxDelegate {
@IBOutlet weak var siCheckBox: CheckBox!
@IBOutlet weak var noCheckBox: CheckBox!
override func viewDidLoad() {
super.viewDidLoad()
self.siCheckBox.delegate = self
self.noCheckBox.delegate = self}
func checkBoxDidChange(checkbox: CheckBox) {
if checkbox == self.siCheckBox {
self.noCheckBox.isChecked = !checkbox.isChecked
} else {
self.siCheckBox.isChecked = !checkbox.isChecked
}}
And this is the subclass that I have assigned to both buttons
protocol CheckBoxDelegate {
func checkBoxDidChange(checkbox: CheckBox) -> Void }
import UIKit
class CheckBox: UIButton {
// Images
let checkedImage = UIImage(named: "check-greenb")! as UIImage
let uncheckedImage = UIImage(named: "check-baseb")! as UIImage
var delegate: CheckBoxDelegate?
// Bool property
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: #selector(CheckBox.buttonClicked(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.isChecked = false
}
func buttonClicked(sender: UIButton) {
isChecked = !isChecked
self.delegate?.checkBoxDidChange(self)
}}