NotificationCenter obersever not called in swift 4

此生再无相见时 提交于 2019-12-24 08:55:10

问题


i am trying to post notification NotificationCenter from appdelegate and receive notification in another view but notification not received.

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 :-

 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.

so any one have solution related to this then please help me.

Thanks in advance.


回答1:


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)


来源:https://stackoverflow.com/questions/47592488/notificationcenter-obersever-not-called-in-swift-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!