我试图将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答案,但我还没有找到任何解决方案.