dimanche 12 mars 2017

Swift 3 XCode 8 - Save button click persistent boolean value

I'm trying to save a button click value as either true or false in persistence. If it is true, a checked box image button is shown. If it is false, an unchecked image button is shown. Initially, an unchecked box image is shown. I am able to get this functionality to work, but I am having difficulty understanding how to get it to persist when I switch to another tab, or close and open the application.

import UIKit

class CurrentGoalViewController: UIViewController {

    var uncheckedBox = UIImage(named: "checkbox")
        var checkedBox = UIImage(named: "checkedbox")

        var isboxclicked: Bool!

This is the checkbox function that occurs when you click on it.

@IBAction func clickBox(_ sender: UIButton) {

    if isboxclicked == true { //box button has not been clicked
        isboxclicked = false
        sender.setImage(#imageLiteral(resourceName: "checkbox"), for: UIControlState.normal) //sets button image to an unchecked (empty) box
    } else {
        isboxclicked = true //box button has been clicked
        sender.setImage(#imageLiteral(resourceName: "checkedbox"), for:UIControlState.normal) //sets button image to an checked (filled) box
        }
}

I set the box button to be false upon loading, so the box button is unchecked upon seeing this screen.

override func viewDidLoad() {
        super.viewDidLoad()

        isboxclicked = false

}

You get to this screen with the checkbox from another screen that is a dynamic tableview, where each word segues to it's own screen with it's own checkbox (it's a goal keeping app).

Basically, I'm reading a lot about persistence and I'm confused on how to save the state of the button so that when you navigate away from the screen or restart the application it is still there. Any help would be appreciated, thanks.




Aucun commentaire:

Enregistrer un commentaire