2016-02-15 37 views
0

我从插座迅速,传球类数据的viewController

数据获取和存储我它的对象 我用NSNotificationCenter。有没有另一种方式将这些数据从套接字类直接传递给Controller?

+0

我假设你正在使用套接字-io的客户端IOS。如果您正在接收响应并将数据设置为响应块中的某个对象,是不是直接将数据发布到所有侦听器?你能详细说明你做了什么以及你在找什么吗? –

+0

我想从插座recived直接从插座视图控制器 –

回答

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) 
     } 
    } 
} 
+0

我使用NSNotificationsCenter 什么是您的解决方案和NSNotificationsCenter之间不同的时候传球 –

+1

更新我的答案做到这一点NSNotifications的方式,它更多的是一个设计决定,代表主要用于1:1的关系和NSNotifications 1:N,这取决于你在找什么。如果多个类正在使用SocketClass,那么请去NSNotifications。 –

4

你得给SEGUE在故事板的标识符。(说vs_segue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){....} 

是要求所有塞格斯被称为表格当前的UIViewController。因此,标识是区分不同塞格斯

然后使用此代码:

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后一个破发点,看看它是否正在对点击/更改视图控制器触发。如果不是,那么故事板中提供的标识符名称必须与此处给出的名称不同。

谢谢

快乐编码。

+0

如果我在某些视图控制器是和recive数据IO 我想通过这些数据来查看控制器 –