Hello,
I'm for some help with using User Defaults to store text saved in my progressLabel and button states. The way this works is that each time the user taps a button, the button toggles "on" and "off" and if "on" a percentage is calculated and displayed in the progressLable as "___ % Complete!". I'd like to save this information so that the user can continually go back in and tap additional buttons as they progress, eventually reaching "100% Complete!".
Here is my code:
class ViewController: UIViewController {
@IBOutlet weak var progressLabel: UILabel!
var countClickedButtons: Int = 0 {
didSet {
let percent = Int(100.0 * Double(countClickedButtons) / 14.0)
progressLabel.text = String(percent) + " % Complete!"
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
/
@IBAction func buttonPressed (_ sender: PRToggleButton) {
if !sender.isSelected {
countClickedButtons += 1
}else{
countClickedButtons -= 1
}
sender.isSelected = !sender.isSelected
}
@IBAction func saveButtonStates(_ sender: Any) {
UserDefaults.standard.set((sender as AnyObject).isSelected, forKey: "isSaved")
//I tried the above and several other ways of using UserDefaults from video tutorials, but so far all I get are crashes. With the above, nothing happens.
}
@IBAction func closeView(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
}