我从socket
获取数据我把它存放在对象中
我使用NSNotificationCenter
。是否有另一种方法可以将这些数据从套接字类直接传递给控制器?
答案 0 :(得分:4)
您必须在故事板中为segue指定一个标识符。(例如 vs_segue )
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){....}
调用从当前UIViewController调用的所有segue。因此标识符用于区分不同的segue
然后使用此代码:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "vs_segue" {
var nextScene = segue.destinationViewController as! VehicleDetailsTableViewController
// Pass the selected object to the new view controller.
nextScene.currentObjectInNextScreen = selectedObjectWhichYouHave
}
}
在var nextScene之后放置一个断点,看看是否在点击/更改ViewController时触发了它。如果不是,那么故事板中提供的标识符名称必须与此处给出的标识符不同。
由于
快乐编码。
答案 1 :(得分:2)
创建ResponseDelegate
协议
protocol ResponseDelegate {
func didReceiveResponse(data:AnyObject?)
}
在套接字类中引用您的委托
class SocketClass {
var responseDelegate:ResponseDelegate //This should be implemented and referred to your ViewController
func getCurrentAmount() {
[socket on:@"currentAmount" callback:^(NSArray* data, SocketAckEmitter* ack) {
responseDelegate.didReceiveResponse(data)
}
}
}
在ViewController
//Set your delegate here
socketClassObject.responseDelegate = self
func didReceiveResponse(data:AnyObject) {
println("Recieved data:\(data)")
}
使用 NSNotificationsCentre
在ViewController中
//Subscribe/Listen for the events
NSNotificationCenter.defaultCenter().addObserver(self, selector: "didReceiveResponse:", name:"CurrentAmountNotification", object: nil)
func didReceiveResponse(notification: NSNotification) {
println("Recieved data:\(notification.userInfo)")
}
在你的SocketClass中
class SocketClass {
func getCurrentAmount() {
[socket on:@"currentAmount" callback:^(NSArray* data, SocketAckEmitter* ack) {
NSNotificationCenter.defaultCenter().postNotificationName("CurrentAmountNotification", object: nil, userInfo:data)
}
}
}