【发布时间】:2016-08-22 23:58:49
【问题描述】:
我正在我的 swift ios 应用程序中实现 socket.io。
目前在几个面板上,我正在监听服务器并等待传入消息。我这样做是通过在每个面板中调用 getChatMessage 函数来实现的:
func getChatMessage(){
SocketIOManager.sharedInstance.getChatMessage { (messageInfo) -> Void in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
//do sth depending on which panel user is
})
}
}
但是我注意到这是一种错误的方法,我需要对其进行更改 - 现在我只想开始侦听传入消息一次,并且当任何消息到来时 - 将此消息传递给任何侦听它的面板。
所以我想通过 NSNotificationCenter 传递传入的消息。到目前为止,我能够传递发生某事的信息,但不能传递数据本身。我是这样做的:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.showSpinningWheel(_:)), name: showSpinner, object: nil)
然后我有一个函数叫做:
func showSpinningWheel(notification: NSNotification) {
}
任何时候我都想称呼它:
NSNotificationCenter.defaultCenter().postNotificationName(hideSpinner, object: self)
那么如何传递对象messageInfo 并将其包含在被调用的函数中?
【问题讨论】:
-
使用用户信息的方法...
NSNotificationCenter.defaultCenter().postNotificationName("hideSpinner", object: nil, userInfo: yourvalue) -
嗯,好的,我怎样才能在该通知调用的函数中获取这个
yourValue(在showSpinningWheel中)? -
像
notification.userinfo一样使用.userinfo
标签: ios swift swift3 nsnotificationcenter nsnotifications