Search code examples
swiftseguensuserdefaultsuiswitchviewdidappear

userDefualt not saving switch when class is segued


My swift code below which is all code no storyboard. Tries to save a switch's state using user defaults. So when the user segues to twoViewController and then segues backs to ViewController. The users switch setting is not being saved. There are no errors presented and I don't know what is going on.

import UIKit

class ViewController: UIViewController {
    var nxtBTn = UIButton()
    var mySwitch =  UISwitch()

    let userDefaults = UserDefaults.standard

    var firstTimeAppLaunch: Bool {
        get {
            // Will return false when the key is not set.
            return userDefaults.bool(forKey: "firstTimeAppLaunch")
        }
        set {}
    }



    @objc func switchAction(_ sender: UISwitch) {
        userDefaults.set(sender.isOn, forKey: "mySwitchValue")
    }
   @objc func press() {
        let segue = twoViewController()

        segue.modalPresentationStyle = .fullScreen // actually .fullScreen would be better
        self.present(segue, animated: true)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        [nxtBTn,mySwitch].forEach {
            $0.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview($0)
            $0.backgroundColor = .green

        }
        if !firstTimeAppLaunch {
            // This will only be trigger first time the application is launched.
            userDefaults.set(true, forKey: "firstTimeAppLaunch")
            userDefaults.set(true, forKey: "mySwitchValue")
        }

        NSLayoutConstraint.activate([

            nxtBTn.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            nxtBTn.topAnchor.constraint(equalTo: view.topAnchor),
            nxtBTn.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 7/7, constant: 0),
            nxtBTn.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.4, constant: 0),

            mySwitch.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            mySwitch.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            mySwitch.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 7/7, constant: 0),
            mySwitch.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.4, constant: 0),

        ])
        mySwitch.addTarget(self, action: #selector(switchAction(_:)), for: .touchDown)
             nxtBTn.addTarget(self, action: #selector(press), for: .touchDown)

        view.backgroundColor = .white
    }

    override func viewDidAppear(_ animated: Bool) {

                mySwitch.isOn = userDefaults.bool(forKey: "mySwitchValue")
    }

}
class twoViewController : UIViewController{

    var backBtn = UIButton()

    override func viewDidLoad() {
        super.viewDidLoad()
        backBtn.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(backBtn)
        backBtn.backgroundColor = .systemGreen


        NSLayoutConstraint.activate([

            backBtn.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            backBtn.topAnchor.constraint(equalTo: view.topAnchor),
            backBtn.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 7/7, constant: 0),
            backBtn.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.4, constant: 0),


        ])

        backBtn.addTarget(self, action: #selector(oneVC), for: .touchDown)


    }

    @objc func oneVC(){
        let segue = ViewController()

        segue.modalPresentationStyle = .fullScreen // actually .fullScreen would be better
        self.present(segue, animated: true)
    }
}

Solution

  • Everywhere you are saying for: .touchDown it is wrong. Change all of them to for: .primaryActionTriggered and things will improve.