moesyzlack23
moesyzlack23

Reputation: 105

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.

solution

In the click action function

let defaults = UserDefaults.standard
defaults.set(isboxclicked, forKey:"checkboxstatus")

In ViewWillAppear

isboxclicked = defaults.bool(forKey: "checkboxstatus")
if isboxclicked == true {
uncheckBox.setImage(checkedBox, for: .normal)
}

Upvotes: 0

Views: 1158

Answers (3)

Ritchie Einstein
Ritchie Einstein

Reputation: 11

NSUserDefault would be my suggestion. It's the one used by apps usually hold persistant data like API keys. So, i think it will solve your requirements too.

Upvotes: 0

Saranjith
Saranjith

Reputation: 11547

You cannot set any property or variable to save the state of check box because it is only available till viewcontroller is in memory.

I would like to suggest you a solution that will persist untill the user re-installs the application. Use UserDefaults values to save the state and you can axhive this feature without any worries.

Also if it is to be saved for limited number of UIViewController instances you can use concept of external vaiables. In swift by default all vaiables will be external vaiables

Hope this helps you,

Upvotes: 0

Herr der Töne
Herr der Töne

Reputation: 178

Save the state when the button is clicked:

let defaults = UserDefaults.standard
defaults.set(true, forKey: "CheckBoxChecked")

Load the state in viewDidLoad:

let defaults = UserDefaults.standard
let state = defaults.bool(forKey: "CheckBoxChecked")

The last function returns false when no value was saved yet.

Upvotes: 1

Related Questions