[英]NotificationCenter obersever not called in swift 4
i am trying to post notification NotificationCenter from appdelegate and receive notification in another view but notification not received.我正在尝试从 appdelegate 发布通知 NotificationCenter 并在另一个视图中接收通知但未收到通知。
Post notification :-发布通知:-
func xmppStream(_ sender: XMPPStream, didReceive message: XMPPMessage)
{
print("did receive message isss-->\(message)")
NotificationCenter.default.post(name: Notification.Name("MessageReceived"), object: message)
}
Received Notification in viewdidload :-在 viewdidload 中收到通知:-
NotificationCenter.default.addObserver(self, selector: #selector(self.messagereceived(notification:)), name: Notification.Name("MessageReceived"), object: nil)
Method :-方法:-
@objc func messagereceived(notification:Notification)
{
let message = notification.object as? XMPPMessage
print("chat conversion message is----->\(message)")
}
My " messagereceived " Method not called.我的“的messageReceived”法不叫。
so any one have solution related to this then please help me.所以任何人都有与此相关的解决方案,然后请帮助我。
Thanks in advance.提前致谢。
Change the notification post from更改通知帖子从
NotificationCenter.default.post(name: Notification.Name("MessageReceived"), object: message)
to到
NotificationCenter.default.post(Notification(name: Notification.Name(rawValue: "MessageReceived"),object: message))
and the observer from和观察者来自
NotificationCenter.default.addObserver(self, selector: #selector(self.messagereceived(notification:)), name: Notification.Name("MessageReceived"), object: nil)
to到
NotificationCenter.default.addObserver(self, selector: #selector(self.messagereceived(notification:)), name: NSNotification.Name(rawValue: "MessageReceived"), object: nil)
In my case i can't get NotificationCenter in some situation in my app .就我而言,在我的应用程序中的某些情况下,我无法获得 NotificationCenter。 and problem was that using
问题是使用
NotificationCenter.default.addObserver(self, selector: #selector(btnRegister(_:)), name: Notification.Name(rawValue: "btnRegister"), object: nil)
in my viewDidLoad() func, And when i set above code in my viewWillAppear() func, that's work perfectly.在我的 viewDidLoad() 函数中,当我在 viewWillAppear() 函数中设置上面的代码时,这是完美的。
According to your code logic , move above code to other func or other section of your code and try to fix your problem.根据您的代码逻辑,将上面的代码移至其他 func 或代码的其他部分,并尝试解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.