adding user defaults to dark mode

北战南征 提交于 2019-12-11 07:25:45

问题


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.


回答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.



来源:https://stackoverflow.com/questions/47622052/adding-user-defaults-to-dark-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!