【问题标题】:NSNotification not observing or posting dataNSNotification 没有观察或发布数据
【发布时间】:2021-03-05 09:08:45
【问题描述】:

我正在尝试学习如何将 NSNotification 用于我正在从事的项目,由于我以前从未使用过它,所以我首先尝试学习如何使用它;每次我尝试遵循 youtube 教程或在线找到的教程时,我的代码似乎都不起作用。此外,在尝试调试问题时,它显示代码的观察者部分没有进入 @obj c 函数。下面是我的代码,展示了它是如何用于发布和观察通知的。

    extension Notification.Name {
    static let notifyId = Notification.Name("NotifyTest")
}

ViewController.swift

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
    }

    var test: ObserverObj = ObserverObj(observerLblText: "Observing")
    @IBAction func notifyObserver(_ sender: Any) {
        let vc = storyboard?.instantiateViewController(identifier: "ObserverVC")
        vc?.modalPresentationStyle = .fullScreen
        guard let vcL = vc else {
            return
        }
        NotificationCenter.default.post(name: .notifyId, object: nil)
        self.navigationController?.pushViewController(vcL, animated: true)
    }
    
}

NotificationTestViewController.swift

import UIKit

class NotificationTestViewController: UIViewController {

    @IBOutlet weak var observerLbl: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(observingFunc(notification:)), name: .notifyId, object: nil)
        // Do any additional setup after loading the view.
    }
    
    @objc func observingFunc(notification: Notification) {
   
        observerLbl.text = "notifying"//text.test.observerLblText
    }

有人可以帮助我,让我知道我哪里出了问题,因为我已经尝试了 2 天。

【问题讨论】:

  • 首先,您的示例非常不实用,因为您可以直接传递数据。最可能的原因是通知是在添加观察者之前发布的。添加打印行以找出答案。通知仅在发送者和接收者不直接相关或有多个接收者时才有用。
  • 我知道通知有用的原因,这就是我需要它们的原因。另外,我只是想在申请之前先练习如何使用它。我一直在添加打印行以查看哪里出错了,但我仍然看不到导致此问题的原因。

标签: ios swift swift5 nsnotificationcenter


【解决方案1】:

在添加观察者之前发送通知,这意味着目标控制器中的viewDidLoad 在源视图控制器中的post 行之后执行。

可能的解决方案是:

  1. 在目标控制器中覆盖 init(coder 并在那里添加观察者。

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        NotificationCenter.default.addObserver(self, selector: #selector(observingFunc), name: .notifyId, object: nil)
    }
    
  2. 如果init(coder没有被调用覆盖init()

  3. 在发布通知之前添加观察者(此解决方案仅用于教育目的)

    @IBAction func notifyObserver(_ sender: Any) {
        guard let vcL = storyboard?.instantiateViewController(identifier: "ObserverVC") as? NotificationTestViewController else { return }
        vcL.modalPresentationStyle = .fullScreen
        NotificationCenter.default.addObserver(vcL, selector: #selector(NotificationTestViewController.observingFunc), name: .notifyId, object: nil)
        self.navigationController?.pushViewController(vcL, animated: true) 
        NotificationCenter.default.post(name: .notifyId, object: nil)
    }
    

但在实践中,强烈建议您不要向您所引用的目的地发送通知。

【讨论】:

    【解决方案2】:

    原因是当你发出通知时,你的NotificationTestViewController还没有调用viewdidload方法

      class ViewController: UIViewController {
            
            override func viewDidLoad() {
                super.viewDidLoad()
                // Do any additional setup after loading the view.
                
            }
    
            var test: ObserverObj = ObserverObj(observerLblText: "Observing")
            @IBAction func notifyObserver(_ sender: Any) {
                let vc = storyboard?.instantiateViewController(identifier: "ObserverVC")
                vc?.modalPresentationStyle = .fullScreen
                guard let vcL = vc else {
                    return
                }
                self.navigationController?.pushViewController(vcL, animated: true) 
                NotificationCenter.default.post(name: .notifyId, object: nil)
            }
            
        }
    

    【讨论】:

    • 不过,谢谢;不明白,怎么调用viewDidLoad方法。当我将代码移动到 viewDidLoad 方法时,我仍然遇到同样的问题。
    • 你不叫它。 UIViewController 在加载视图后调用它。然而,即使这个答案颠倒了行的顺序,我怀疑在发布通知时是否加载了视图。您可以通过调用vcL.loadViewIfNeeded() 来强制加载视图,或者您可以使用dispatchQueue.main.async 来稍微延迟通知的发布。
    猜你喜欢
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多