M.I M.I -3 years ago 109
iOS Question

User defaults Only Working On Restart

I am trying to implement a light and dark mode in my application. In the

settingsViewController
I have these lines of code:

//Sets user default for colour
let lightMode = UserDefaults.standard.bool(forKey: "lightMode")

//UISegment control for user to pick colour
@IBOutlet var colourSegment: UISegmentedControl!

//Updates lightMode based on user selection
@IBAction func didChangeColours(_ sender: Any) {

if colourSegment.selectedSegmentIndex == 0 {
UserDefaults.standard.set(true, forKey: "lightMode")
} else if colourSegment.selectedSegmentIndex == 1 {
UserDefaults.standard.set(false, forKey: "lightMode")
}

}


In my
entryViewController
, in my
viewDidLoad
, I have:

let lightMode = UserDefaults.standard.bool(forKey: "lightMode")

if lightMode == false {
Colours.darkMode()
}

customisations()


The issue that I'm running into is that for some reason, my application is only changing it's colour scheme after I restart it. That is, if the user selects the
darkIndex
of the
colourSegment
, the application only updates the colour after I restart. I am wondering what is the solution for this.

Answer Source

The problem is in the line -

//Sets user default for colour
let lightMode = UserDefaults.standard.bool(forKey: "lightMode")

this line is not for setting the Userdefaults but instead it gets the UserDefaults. Since you use it before setting the default Value, it doesn't reflect the right segmented choice. So your setting part is correct and you should fetch the value only after you have set it.

Also in your entryViewController, instead of using it from settingsVC, do below -

//get from UserDefaults
let lightMode = UserDefaults.standard.bool(forKey: "lightMode")

//Compare the previous fetched value
if lightMode == false {
    Colours.darkMode()
}

//This function sets the colour for the elements
colours()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download