NSNotification 不观察或发布数据

NSNotification not observing or posting data

我正在尝试学习如何将 NSNotification 用于我正在从事的项目,但由于我以前从未使用过它,因此我首先尝试学习如何使用它;每次我尝试按照 youtube 教程或在线找到的教程进行操作时,我的代码似乎都无法正常工作。此外,在尝试调试问题时,它显示代码的观察者部分未进入 @obj c 函数。下面是我的代码,展示了它是如何被用来 post 并观察通知的。

    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 天了。

原因是当你发出通知时,你的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 在源视图控制器中的 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)
    }
    

但是在实践中,强烈建议您不要向您参考的目的地发送通知。