我希望有人帮我解决这个问题,我有更好的运气:
我有一个 UIPickerView ,用户在其中进行选择,然后按下按钮。我可以很高兴地获得用户的选择,如我的 NSLog 所示,完成后,我想向另一个视图控制器发送通知,该控制器将显示一个带有所选选项的标签。好吧,虽然看起来一切都做对了,但不知何故它不起作用,标签保持不变。这是代码:
广播公司:
if ([song isEqualToString:@"Something"] && [style isEqualToString:@"Other thing"])
{
NSLog (@"%@, %@", one, two);
[[NSNotificationCenter defaultCenter] postNotificationName:@"Test1" object:nil];
ReceiverViewController *receiver = [self.storyboard instantiateViewControllerWithIdentifier:@"Receiver"];
[self presentModalViewController:receiver animated:YES];
}
观察员:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification) name:@"Test1" object:nil];
}
return self;
}
-(void)receiveNotification:(NSNotification*)notification
{
if ([[notification name] isEqualToString:@"Test1"])
{
[label setText:@"Success!"];
NSLog (@"Successfully received the test notification!");
}
else
{
label.text = @"Whatever...";
}
}