ios - NSNotificationCenter 和 UIPickerView 的问题

标签 ios uipickerview nsnotifications nsnotificationcenter uipicker

我希望我能在这方面帮助我的人有更好的运气:

我有一个 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/

相关文章:

ios - 完全删除 View Controller - Swift

swift - 在使用 2 个不同的 ViewController 时解包 Optional 值时,自定义类抛出错误意外发现 nil

ios - 如何在 UIPicker 中首先选择具有特定标题的数组

iphone - 如何在按下按钮后以动画方式显示 UIPickerView?

ios - NSNotification MPVideoplayercontroller 崩溃 - ARC

swift - 键盘问题上方的文本字段

ios - 过去某个日期的 NSNotifications

ios - MarqueeLabel 文本不会滚动

ios - 如何使用 Google Sign In 保持用户登录状态?

ios - 如何从 UICollectionView 中删除行?