【发布时间】:2015-11-30 14:08:40
【问题描述】:
试图让一个 ViewController 通过标准的 Cocoa Notifications 与另一个 ViewController 通信。
写了一个简单的测试用例。在我最初的 VC 中,我将以下内容添加到 viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self
selector: @selector(mesageReceived:)
name:@"test.channel.pw" object:nil];
我添加以下方法:
- (void)mesageReceived:(NSNotification *)notif
{
NSString *text = (NSString *)notif;
}
然后我转到一个在其 viewDidLoad 中添加了以下内容的 VC:
NSString *test = @"someText";
[[NSNotificationCenter defaultCenter]
postNotificationName:@"test.channel.pw" object:test];
当我运行它时,通知起作用了——messageReceived 方法被调用——但是文本的值是“test.channel.pw”,而不是我预期的“someText”。
这里发生了什么,使用通知传递对类实例的引用的正确语法是什么?
【问题讨论】:
标签: ios cocoa nsnotificationcenter nsnotification