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
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")
}
}