我希望我能在这方面帮助我的人有更好的运气:
我有一个 UIPickerView,用户可以在其中进行选择,然后按下按钮。我可以很高兴地获得用户的选择,如我的 NSLog 中所示,完成后,我想向另一个 View Controller 发送通知,该 View Controller 将显示带有所选选项的标签。好吧,虽然看起来一切都做对了,但不知何故它不起作用并且标签保持完好无损。这是代码:
广播员:
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...";
}
}
最佳答案
我认为您的选择器中存在语法错误:@selector(receiveNotification)
。它可能应该是带有冒号的 @selector(receiveNotification:)
,因为您的方法接受 NSNotification *notification
消息。没有它,这是一个不同的签名。
关于ios - NSNotificationCenter 和 UIPickerView 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8448680/