vendredi 28 février 2020

Persistent checkbox - Swift for MacOS project

I have been looking to solve this problem for a while but I'm finding only solutions for IOS projects.

I'm building a little macOS application with swift and I would like to make the state of my checkbox persistent, so when I open the application back the state of the checkbox didn't change.

Currently I'm trying to use the UserDefaults.standard to save the value in a persistent way

class ViewController: NSViewController, NSTextFieldDelegate{
        @IBOutlet weak var LoginItemButton: NSButton!
        var checkButtonState = UserDefaults.standard

       @IBAction func LoginItemPressed(_ sender: NSButton) {
            checkButtonState.set(sender.state, forKey: "buttonState") }}

Now I would like with that saved value to assign it back to the sender.state, and keep it that way!

But whatever I do the checkbox goes off every time I restart the app, even if I set it manually with sender.state = .on

Do you have any suggestions?

This is the full application




Aucun commentaire:

Enregistrer un commentaire