将用户默认设置为暗模式 [英] adding user defaults to dark mode

查看:80
本文介绍了将用户默认设置为暗模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是之前一篇博文的延续.我想知道的是如何在整个应用程序中为暗模式添加用户默认值.请不要注意我上一篇文章中说 UserDefaults 的代码,我正在关注 教程 只是复制了他所做的,对用户默认值一无所知.整个黑暗模式在整个应用程序中都能很好地工作.我只需要知道如何执行所有用户默认设置.如果您有任何问题,请随时提出.

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

我正在寻找的是如何将用户默认值添加到暗模式.所以一旦开启黑暗模式,那么当你关闭应用程序时,它会保持开启等等.

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.

推荐答案

您使用 NSUserDefaults 所做的一切都是为了存储和检索设置.您将在其中存储您的用户使用的主题.

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

在主题化视图控制器的 viewWillAppear 中,您只需检查您在 UserDefaults 中指定的键的值.

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.

推荐阅读:UserDefaults

顺便说一句,您在 YouTube 上关注的教程系列显然对初学者来说不够好,因为它提到了 UserDefaults 甚至使用了它们,但显然从未告诉您如何使用它们的事实证明了这一点.你应该找一本关于 iOS 开发的好书.

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.

这篇关于将用户默认设置为暗模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆