小编Ros*_*osh的帖子

NSNotification不会调用@selector方法

我试图将NSString从一个类传递到另一个类.假设我有ViewController A和ViewController B.我想将NSString从A传递给B.

在ViewController A中,我有以下代码:

[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationMessageEvent" object:userType];
Run Code Online (Sandbox Code Playgroud)

//这里用户类型是我使用委托获得的字符串,我需要将此userType传递给ViewController B.

在ViewController B中,我有以下代码:在viewDidLoad中,我有以下代码:

[NSNotificationCenter defaultCenter]
     addObserver:self selector:@selector(notificationAction:) name:@"NotificationMessageEvent" object:nil];
Run Code Online (Sandbox Code Playgroud)

//调用此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.");
    }
}
Run Code Online (Sandbox Code Playgroud)

//从不调用上面的选择器方法.

我已经阅读了其他类似的stackoverflow答案,但我还没有找到任何解决方案.

iphone xcode objective-c nsnotificationcenter ios

0
推荐指数
1
解决办法
1969
查看次数

标签 统计

ios ×1

iphone ×1

nsnotificationcenter ×1

objective-c ×1

xcode ×1