1

This is a continuation of an earlier post. What I was wondering was how to add the user defaults for the dark mode throughout the app. Please do not pay attention for the code that says UserDefaults in my last post, I was following a tutorial and just kind of copied what he did, not knowing anything at all about User Defaults. The whole dark mode works beautifully throughout the app. I just need to know how to do all the user defaults. If you have any questions feel free to ask.

The code below is what the custom cell looks like below that is in a settings view controller, to change the app to a Dark Mode. Everything works great and as it should. I just need to put in the user defaults into the actions.

import UIKit

class DarkModeTableViewCell: UITableViewCell {
    var DarkisOn = Bool()
    let userDefaults = UserDefaults.standard

    @IBOutlet var darkModeSwitchOutlet: UISwitch!

    override func awakeFromNib() {
        super.awakeFromNib()

        NotificationCenter.default.addObserver(self, selector: #selector(darkModeEnabled(_:)), name: .darkModeEnabled, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(darkModeDisabled(_:)), name: .darkModeDisabled, object: nil)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    @IBAction func darkModeSwitched(_ sender: Any) {
        if darkModeSwitchOutlet.isOn == true {
            //enable dark mode
            DarkisOn = true

            //add a userDefault here so that the app will stay in dark mode
            NotificationCenter.default.post(name: .darkModeEnabled, object: nil)
        } else {
            //enable light mode
            DarkisOn = false

            //add a userDefault here so that the app will stay in light mode
            NotificationCenter.default.post(name: .darkModeDisabled, object: nil)
        }
    }

    @objc private func darkModeEnabled(_ notification: Notification) {
        DarkModeTableViewCellChange.instance.set(for: self)
        textLabel?.textColor = UIColor.white
    }

    @objc private func darkModeDisabled(_ notification: Notification) {
        LightModeTableViewCellChange.instance.set(for: self)
        textLabel?.textColor = UIColor.black
    }
}

EDIT: What I am looking for is how to add the user defaults to the dark mode. So once the dark mode is turned on, then when you close the app, it would stay on, etc.

Jaqueline
  • 465
  • 2
  • 8
  • 25
  • 3
    Instead of posting a link to another question, [edit] this question to stand on its own. Make this a clear, concise, self-contained question. – rmaddy Dec 03 '17 at 19:00
  • Is that better? – Jaqueline Dec 03 '17 at 19:10
  • Code above is not correctly indented and contains useless empty lines. Please make easy readable as much as possible. – meaning-matters Dec 03 '17 at 19:24
  • Well it's easier to read it when there are spaces between the lines... and I just indented it more for you – Jaqueline Dec 03 '17 at 19:29
  • There's a lot of extraneous words here. Instead of telling us what to ignore, please [edit] and just remove it. Then be more specific: what's the core of what you're asking? How to store a flag in user defaults? – jscs Dec 16 '17 at 23:18
  • I've just modified the question to better say what I need done. – Jaqueline Dec 16 '17 at 23:21

1 Answers1

1

Everything you do with NSUserDefaults is to store settings and retrieve them. You would store what theme your user is using in them.

So do something like this when changing your themes (in your previous question you were already doing something like this):

let defaults = UserDefaults.standard

// Do something like this when using changing your theme to dark mode.
defaults.set(true, "darkModeEnabled")

// Do something like this when changing your theme to your standard one
defaults.set(false, "darkModeEnabled")

In the viewWillAppear of your themable view controllers, you just check the value of the key you specified in UserDefaults.

/// Check if the user is using dark mode in viewDidLoad.
override func viewWillAppear() {
    super.viewDidLoad()

    let darkModeEnabled = defaults.bool(forKey: "darkModeEnabled")

    if darkModeEnabled {
        // Apply your dark theme
    } else {
        // Apply your normal theme.
    }
}

This way your app your view will controllers will have the right theme upon loading, and the user will see the right one when loading the app.

Recommended reading: UserDefaults

As an aside note, the tutorial series you are following on YouTube is clearly not good enough for beginners, as it can be evidenced by the fact it mentions UserDefaults and even uses them but apparently never tells you how to use them. You should just get a good book on iOS development instead.

Andy Ibanez
  • 12,104
  • 9
  • 65
  • 100
  • Thank you so much! I will check this out tomorrow as it is 1 am here. The tutorial I was watching was part 1 of part 2 which was never made so he never did anything beyond what I did with user defaults – Jaqueline Dec 29 '17 at 09:22