I’m new to swift so I am stumped on something that is probably very simple.
I want to have checkboxes for each day of the week and be able to toggle their states a bit like the standard Apple clock app alarm repeat page. I am using a subclass borrowed from http://ift.tt/1MwLmW2 to create a set of checkboxes. All that works like a charm in the storyboard but now I am struggling with how to refer to the subclass checkbox ‘state’ in my view controller to actually do something.
So here is the subclass:
import UIKit
class checkBox: UIButton {
//images
let checkedImage = UIImage(named: "checkBoxChecked")
let unCheckedImage = UIImage(named: "checkBoxUnchecked")
//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: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
self.isChecked = false
}
func buttonClicked(sender:UIButton) {
if(sender == self){
if isChecked == true{
isChecked = false
}else{
isChecked = true
}
}
}
}
Here is an outlet example:
@IBOutlet weak var sun: checkBox!
Here is my empty array ready to be appended (working):
var daysArray:[String] = []
Here is an example action:
@IBAction func setSunday(sender: checkBox) {
if (****what do I do here to retrieve the button state?****) {
daysArray.append("0")
print(daysArray) // Check the array is working then delete
} else {
daysArray.removeAtIndex(0)
print(daysArray) // Check the array is working then delete
}
}
If I can get this working then I can apply to all days of the week and make the state of the array persistent using NSUserDefaults. Hope you guys can help.
Aucun commentaire:
Enregistrer un commentaire