I am implementing a UIButton
(checkbox) into a custom prototype UITableViewCell
subclass. The functionality of the check/unchecked state works, but for the life of me, even with all the tutorials and questions asked on here, I cannot get the state of the button to save in NSUserDefaults
.
My ViewController
:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var defaults = NSUserDefaults.standardUserDefaults()
override func viewDidLoad() {
super.viewDidLoad()
//NSUserDefaults go here
checked = defaults.boolForKey("boxIsChecked")
}
My UITableViewCell
subclass:
class TableViewCell: UITableViewCell {
let isSelected = UIImage(named: "Selected")
let unselected = UIImage(named: "Unselected")
@IBOutlet weak var checkBoxButton: UIButton!
@IBAction func checkBox(sender: UIButton) {
var defaults = NSUserDefaults.standardUserDefaults()
if (!checked) {
checked = true
checkBoxButton.setImage(isSelected, forState: UIControlState.Normal)
defaults.setBool(checked, forKey: "boxIsChecked")
}
else if (checked) {
checked = false
checkBoxButton.setImage(unselected, forState: UIControlState.Normal)
defaults.setBool(checked, forKey: "boxIsChecked")
}
}
func checkTheBox() {
if (!checked) {
checkBoxButton.setImage(unselected, forState: UIControlState.Normal)
}
else if (checked) {
checkBoxButton.setImage(isSelected, forState: UIControlState.Normal)
}
}
}
Aucun commentaire:
Enregistrer un commentaire