NSNotification 没有调用@selector 方法
NSNotification is not calling @selector method
我正在尝试将 NSString 从一个 class 传递到另一个。假设我有 ViewController A 和 ViewController B。我想将 NSString 从 A 传递到 B。
在 ViewController A 中,我有以下代码:
[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationMessageEvent" object:userType];
//这里的用户类型是我使用delegate得到的字符串,我需要把这个userType传给ViewController B
在ViewController B 中,我有以下代码:
在 viewDidLoad 中,我有以下代码:
[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(notificationAction:) name:@"NotificationMessageEvent" object:nil];
//调用了这个NSNotificationCenter方法
我已经注册了以下选择器方法。
-(void) notificationAction:(NSNotification *) notification
{
if ([notification.object isKindOfClass:[NSString class]])
{
NSString *message = [notification object];
// do stuff here with your message data
NSLog(@"%@ is message",message);
}
else
{
NSLog(@"Error, object not recognised.");
}
}
//永远不会调用上面的选择器方法。
我已经阅读了其他类似的 Whosebug 答案,但我无法找到与此相关的任何解决方案。
您的代码和语法显然是正确的。我猜这是对象生命周期的问题。我假设以下任一情况为真:
- ViewController发布通知时B实际上并不作为对象存在
或
- ViewController A 在 ViewController B 有机会注册之前发布通知。
验证其中任何一个的一种方法是添加两个断点,一个是发布通知的地方,另一个是注册通知侦听器的地方。应在发布通知之前命中注册侦听器的断点。如果发生这种情况,请验证 ViewController B 在发布通知时实际上是一个存在的对象(比如它没有从导航堆栈弹出或其他东西)。
这就是您要找的
NSNotification not being sent when postNotificationName: called
您必须在 postNotificationName
之前添加 Observer
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:@"NotificationMessageEvent" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationMessageEvent" object:nil];
我正在尝试将 NSString 从一个 class 传递到另一个。假设我有 ViewController A 和 ViewController B。我想将 NSString 从 A 传递到 B。
在 ViewController A 中,我有以下代码:
[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationMessageEvent" object:userType];
//这里的用户类型是我使用delegate得到的字符串,我需要把这个userType传给ViewController B
在ViewController B 中,我有以下代码: 在 viewDidLoad 中,我有以下代码:
[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(notificationAction:) name:@"NotificationMessageEvent" object:nil];
//调用了这个NSNotificationCenter方法
我已经注册了以下选择器方法。
-(void) notificationAction:(NSNotification *) notification
{
if ([notification.object isKindOfClass:[NSString class]])
{
NSString *message = [notification object];
// do stuff here with your message data
NSLog(@"%@ is message",message);
}
else
{
NSLog(@"Error, object not recognised.");
}
}
//永远不会调用上面的选择器方法。
我已经阅读了其他类似的 Whosebug 答案,但我无法找到与此相关的任何解决方案。
您的代码和语法显然是正确的。我猜这是对象生命周期的问题。我假设以下任一情况为真:
- ViewController发布通知时B实际上并不作为对象存在
或
- ViewController A 在 ViewController B 有机会注册之前发布通知。
验证其中任何一个的一种方法是添加两个断点,一个是发布通知的地方,另一个是注册通知侦听器的地方。应在发布通知之前命中注册侦听器的断点。如果发生这种情况,请验证 ViewController B 在发布通知时实际上是一个存在的对象(比如它没有从导航堆栈弹出或其他东西)。
这就是您要找的 NSNotification not being sent when postNotificationName: called 您必须在 postNotificationName
之前添加 Observer[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:@"NotificationMessageEvent" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationMessageEvent" object:nil];