快速未通知NotificationCenter的所有人员 [英] NotificationCenter obersever not called in swift 4
本文介绍了快速未通知NotificationCenter的所有人员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从appdelegate发布通知NotificationCenter并在另一个视图中接收通知,但未收到通知.
i am trying to post notification NotificationCenter from appdelegate and receive notification in another view but notification not received.
发布通知:-
func xmppStream(_ sender: XMPPStream, didReceive message: XMPPMessage)
{
print("did receive message isss-->\(message)")
NotificationCenter.default.post(name: Notification.Name("MessageReceived"), object: message)
}
viewdidload中收到的通知:-
Received Notification in viewdidload :-
NotificationCenter.default.addObserver(self, selector: #selector(self.messagereceived(notification:)), name: Notification.Name("MessageReceived"), object: nil)
方法:-
@objc func messagereceived(notification:Notification)
{
let message = notification.object as? XMPPMessage
print("chat conversion message is----->\(message)")
}
未调用我的"收到消息"方法.
所以任何人都有与此相关的解决方案,然后请帮助我.
so any one have solution related to this then please help me.
谢谢.
推荐答案
从以下位置更改通知帖子
Change the notification post from
NotificationCenter.default.post(name: Notification.Name("MessageReceived"), object: message)
到
NotificationCenter.default.post(Notification(name: Notification.Name(rawValue: "MessageReceived"),object: message))
和观察者
NotificationCenter.default.addObserver(self, selector: #selector(self.messagereceived(notification:)), name: Notification.Name("MessageReceived"), object: nil)
到
NotificationCenter.default.addObserver(self, selector: #selector(self.messagereceived(notification:)), name: NSNotification.Name(rawValue: "MessageReceived"), object: nil)
这篇关于快速未通知NotificationCenter的所有人员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文