Fady Ilias
Fady Ilias

Reputation: 31

How do i keep UISwitch state when changing ViewControllers?

When I move from one view controller to another, the switch on the first controller resets itself and does not retain its state. How can I make it save its state when come back to it after viewing other controllers? And how do I make it save its state after closing the app. I have looked at the various stackOverflow questions and responses and the apple documentation, but nothing seems to work.

Here is my class for the View Controller that has the switch.

class Days: UIViewController {
    @IBOutlet weak var switchButton: UISwitch!
    var switchState = true
    let switchKey = "switchState"
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    @IBAction func saveSwitchPressed(sender:AnyObject) {
        if self.switchButton.on {
            self.switchState = true
            NSUserDefaults.standardUserDefaults().setBool(self.switchState, forKey: switchKey)
            NSUserDefaults.standardUserDefaults().synchronize()
            println(NSUserDefaults.standardUserDefaults().boolForKey(switchKey))
        } else {
            self.switchState = false
            NSUserDefaults.standardUserDefaults().setBool(self.switchState, forKey: switchKey)
            NSUserDefaults.standardUserDefaults().synchronize()
            println(NSUserDefaults.standardUserDefaults().boolForKey(switchKey))
        }
    }
}

I'm a beginner to Swift and generally Xcode. Thank you in advance for your time and help :)

Upvotes: 2

Views: 9134

Answers (3)

pankaj nigam
pankaj nigam

Reputation: 401

just after code connect UISwitch to IBoutlet.

class ViewController: UIViewController {

    @IBOutlet weak var switchButton: UISwitch!

    @objc func switchStateDidChange(_ sender:UISwitch!)
    {
        if (sender?.isOn == true){
            print("on")
        }
        else {
            print("off")
        }
        UserDefaults.standard.set(sender.isOn, forKey: "switchState")
        UserDefaults.standard.synchronize()
    }

   override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(false)
        switchButton?.isOn =  UserDefaults.standard.bool(forKey: "switchState")
        switchButton?.addTarget(self, action: #selector(switchStateDidChange(_:)), for: .touchUpInside)
    }
}

Upvotes: 0

Leo Dabus
Leo Dabus

Reputation: 236260

Xcode 8.3 • Swift 3.1

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var switchButton: UISwitch!

    override func viewDidLoad() {
        super.viewDidLoad()
        switchButton.isOn =  UserDefaults.standard.bool(forKey: "switchState")
    }

    @IBAction func saveSwitchPressed(_ sender: UISwitch) {
        UserDefaults.standard.set(sender.isOn, forKey: "switchState")
    }
}

enter image description here

Upvotes: 15

mittens
mittens

Reputation: 746

Since you're syncing the on/off state of the switch you could on viewWillAppear: or viewDidAppear: set the state of the switch to the value stored in NSUserDefaults

Something along the lines of

override func viewWillAppear(animated: Bool) {
  self.switchButton.on = NSUserDefaults.standardUserDefaults().boolForKey(switchKey)
}

Upvotes: 1

Related Questions