是的,通知是一个很好的方法。当一个模型想要更新控制器[即ViewController] - 通知是一个很好的方法。在我的情况下,我试图发现使用SSDP(使用AsyncUdpSocket)的设备,并且我想在发现设备时更新/通知我的视图控制器。由于这是异步的,当收到数据时,我使用了通知。下面是一个简单的事情,我做的事:
在viewDidLoad中(我想替换init但是这并没有为我工作好) - 我登记我的ViewController一个通知如下:
*NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(foundController:)
name:@"DiscoveredController"
object:nil];
这里在我的ViewController选择:
// receive the notification
- (void)foundController:(NSNotification *)note
{
self.controllerFoundStatus.text = @"We found a controller";
}
在我的“模型” [未在App代表 - 我创造,我用它来发现设备的新类“serviceSSDP”我所做的只是张贴通知如下:
[[NSNotificationCenter defaultCenter] postNotificationName:@"DiscoveredController" object:nil];
就是这样。当我收到我的SSDP发现的正确响应时[通常是在AsyncUdpSocket的:
- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock
didReceiveData:(NSData *)data
withTag:(long)tag
fromHost:(NSString *)host
port:(UInt16)port
]中。
谢谢尼古拉,我会检查通知中心。起初,我只是担心使用通知中心的含义使用不必要的系统资源。 – Ben 2009-06-03 23:07:48