-3

I've got 4 SwichButton, this is standard to Off.

How can I save the setting when a switch button is set to on ?

I have this:

        @IBAction func SwitchNofic(_ sender: UISwitch) {

    let switchTag = sender.tag

    if (switchTag == 1) && (sender.isOn == true){
        print("1")
        createNoficationMorgen()
    }else if (switchTag == 2) && (sender.isOn == true){
        print("2")
        createNoficationMittag()
    }else if (switchTag == 3) && (sender.isOn == true){
        print("3")
        createNoficationAbend()
    }else if (switchTag == 4) && (sender.isOn == true){
        print("4")
        createNoficationNacht()
    }

}
jscs
  • 63,694
  • 13
  • 151
  • 195
Daniel.P
  • 75
  • 1
  • 8
  • I think you can look this link it would help you. [Releated Link](https://stackoverflow.com/questions/24714921/how-to-link-a-boolean-value-to-the-on-off-state-of-a-uiswitch) – Mertalp Tasdelen Feb 15 '19 at 23:37
  • 2
    Try something. Apply the answers that you just got two hours ago. It's the same fundamental problem. – jscs Feb 16 '19 at 00:53

1 Answers1

2

It depends if you want to save it just for the time the app is alive.

Just use a boolean variable so you know what is on and what is not, also each switchButton has a property isOn.

If you want it to be persistant try UserDefaults.

Swift 4

To save permanently :

UserDefaults.standard.set(true, forKey: “isDarkModeKey”)

To retrieve (this should be called in viewDidAppear):

let isDarkModeEnabled = UserDefaults.standard.bool(forKey: “isDarkModeKey”)

if isDarkModeEnabled {
   mySwicth.setOn(true, animated : false)
   //set the background to dark
} else {
   mySwicth.setOn(false, animated : false)
   //set the background to white
}
Mathias
  • 556
  • 9
  • 21