在不加载视图的情况下接收通知 [英] Receiving a notification without loading the view

查看:73
本文介绍了在不加载视图的情况下接收通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我在这个问题中所解释的,我试图通过从 TableView 中的 UISwitch 到另一个 ViewController 的数据.但是,我希望在关闭设置视图(包含 UISwitch)时传递数据.按照这个伟大的答案的第三步,我使用 UserDefaults 传递数据并尝试使用 NotificationCenter 更新关闭设置视图时的 UserDefaults 值.

As I explained in this question, I'm trying to pass data from a UISwitch in a TableView to another ViewController. However, I want the data to be passed when the Settings View (which contains the UISwitch) is dismissed. Following the third step of this great answer, I'm using UserDefaults to pass the data and trying to use NotificationCenter to update the UserDefaults values when dismissing the Settings View.

要在用户默认值更改时更新,您可以让设置控制器在关闭时发布通知.然后,您应用中的其他 VC 可以侦听此通知并根据需要进行更新.

我尝试在 viewDidLoad 中检索通知,但该值没有刷新,因为关闭设置视图不会每次都加载新的 ViewController.

I've tried retrieving the notification inside viewDidLoad, but the value doesn't get refreshed because dismissing the Settings View doesn't load the new ViewController everytime.

如何在每次关闭设置视图时接收/阅读通知?更准确地说,我将在哪里以及如何编写用于读取通知的代码?

How can I receive/read a notification every time the Settings View is dismissed? More precisely, where and how would I write the code for reading the notification?

这是发布通知的代码:

class SettingsViewController: UIViewController {

@IBAction func doneButton(_ sender: UIButton) {

    self.dismiss(animated: true, completion: nil)
    NotificationCenter.default.post(name: Notification.Name("nSound"), object: nil)

}

这是检索通知的代码:

    class ViewController: UIViewController {

    @IBOutlet weak var testLabel: UILabel!    
    var savedValue = UserDefaults.standard.bool(forKey: "sound")


    override func viewDidLoad() {
        super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.viewDidLoad), name: Notification.Name("nSound"), object: nil)

//refresh the savedValue variable       
        savedValue = UserDefaults.standard.bool(forKey: "sound")

//test the savedValue variable
        if (savedValue == true) {
            testLabel.text = "yes"
        } else {
            testLabel.text = "no"
        }

    }

推荐答案

每次我关闭设置视图时,变量savedValue 都不会自行刷新.这是因为另一个 ViewController 没有每次都加载自己.我所做的是创建了一个新函数,该函数在调用通知时运行(即关闭设置视图时).代码如下:

The variable savedValue wasn't refreshing itself each time I dismissed the Settings View. This is because the other ViewController wasn't loading itself each time. What I did is I created a new function that runs when the notification is called (which is when Settings View is dismissed). Here is the code:

class ViewController: UIViewController {

@IBOutlet weak var testLabel: UILabel!    
var savedValue = UserDefaults.standard.bool(forKey: "sound")


override func viewDidLoad() {
        super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.retrieveValue), name: Notification.Name("nSound"), object: nil)

//For when the view appears the first time
    savedValue = UserDefaults.standard.bool(forKey: "sound")

    if (savedValue == true) {
        testLabel.text = "yes"
    } else {
        testLabel.text = "no"
    }

}

func retrieveValue() {

//refresh the savedValue variable       
            savedValue = UserDefaults.standard.bool(forKey: "sound")

//test the savedValue variable
            if (savedValue == true) {
                testLabel.text = "yes"
            } else {
                testLabel.text = "no"
            }

    }

请注意,addObserver 现在调用函数 retrieveValue().

Note that addObserver now calls the function retrieveValue().

这篇关于在不加载视图的情况下接收通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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