Ios 未观察或发布数据的通知

Ios 未观察或发布数据的通知,ios,swift,swift5,nsnotificationcenter,Ios,Swift,Swift5,Nsnotificationcenter,我正在努力学习如何在我正在从事的项目中使用NSNotification,因为我以前从未使用过它,所以我首先尝试学习如何首先使用它;每次我尝试跟随youtube教程或在线找到的教程时,我的代码似乎都不起作用。此外,当试图调试该问题时,它显示代码的观察者部分没有进入@obj c函数。下面是我的代码,显示了如何使用它发布和观察通知 extension Notification.Name { static let notifyId = Notification.Name("No

我正在努力学习如何在我正在从事的项目中使用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)
    }
    
}
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
    }
NotificationTestViewController.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)
    }
    
}
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
    }

有人能帮我一下,让我知道我在哪里出了问题,因为我已经试了两天。

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

类ViewController:UIViewController{
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后执行任何其他设置。
}
var测试:ObserverObj=ObserverObj(observerLblText:“观察”)
@iAction func notifyObserver(\发送方:任意){
让vc=storyboard?.InstanceEviewController(标识符:“ObserverVC”)
vc?.modalPresentationStyle=.fullScreen
保护让vcL=vc else{
返回
}
self.navigationController?.pushViewController(vcL,动画:true)
NotificationCenter.default.post(名称:.notifyId,对象:nil)
}
}

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

可能的解决办法是:

  • 覆盖目标控制器中的
    init(编码器
    ),并在那里添加观察者

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        NotificationCenter.default.addObserver(self, selector: #selector(observingFunc), name: .notifyId, object: nil)
    }
    
  • 如果未调用
    init(编码器
    重写
    init()

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


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

    首先,您的示例非常不实用,因为您可以直接传递数据。最可能的原因是,通知是在添加观察者之前发布的。添加打印行以解决此问题。否t只有在发送方和接收方没有直接关系或存在多个接收方的情况下,通知才有用。我知道通知之所以有用的原因,这也是我需要通知的原因。此外,我只是在申请前尝试先练习如何使用通知。我一直在添加打印行,以查看哪里出错,但我仍然不知道自己在做什么这就导致了这个问题。谢谢你,但是,我不明白,我该如何调用viewDidLoad方法。当我将代码移动到viewDidLoad方法时,我仍然会遇到同样的问题。你不会调用它。
    UIViewController
    在加载视图后调用它。但是,即使这个答案颠倒了行的顺序,我怀疑它不会在发布通知时加载视图。您可以通过调用
    vcL.loadViewIfNeeded()
    强制加载视图,也可以使用
    dispatchQueue.main.async
    稍微延迟通知的发布。