NSNotificationCenter 和 UIPickerView 的问题

发布于 2024-12-20 10:41:14 字数 1225 浏览 5 评论 0原文

我希望有人能帮助我解决这个问题:

我有一个 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...";
}


}

I hope I have better luck with someone helping me on this one:

I have a UIPickerView where a user makes a selection and then presses a button. I can gladly obtain the users choice, as shown in my NSLog, and when this is done, I want to send a notification to another view controller that will show a label with the option selected. Well, although it seems everything is done right, somehow it does not work and the label stays intact. Here is the code:

Broadcaster:

 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];

}

Observer:

    - (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...";
}


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨落星ぅ辰 2024-12-27 10:41:14

我认为您的选择器中存在语法错误:@selector(receiveNotification)。它可能应该是带有冒号的@selector(receiveNotification:),因为您的方法接受NSNotification *notification消息。没有它,它就是一个不同的签名。

I think you have a syntax error in your selector: @selector(receiveNotification). It should probably be @selector(receiveNotification:) with the colon since your method accepts the NSNotification *notification message. Without it, it's a different signature.

诗笺 2024-12-27 10:41:14

问题可能是通知是在与主线程不同的线程上发送(并因此接收)的。只有在主线程上您才能更新 UI 元素(如标签)。

请参阅我对 这个问题了解线程和 NSNotifications。

使用类似以下内容:

NSLog(@"Code executing in Thread %@",[NSThread currentThread] );

将主线程与执行 receiveNotifcation: 方法的位置进行比较。

如果您在非主线程的线程上发送通知,则解决方案可能是在主线程上广播您的 nsnotifications,如下所示:

//Call this to post a notification and are on a background thread      
- (void) postmyNotification{
  [self performSelectorOnMainThread:@selector(helperMethod:) withObject:Nil waitUntilDone:NO];
}

//Do not call this directly if you are running on a background thread.
- (void) helperMethod{
  [[NSNotificationCenter defaultCenter] postNotificationName:@"SOMENAME" object:self];
}

如果您只关心主线程上更新的标签线程,您可以使用类似以下内容在主线程上执行该操作:

dispatch_sync(dispatch_get_main_queue(), ^(void){
                            [label setText:@"Success!"];
                        });

希望有帮助!

The issue is likely that the notification is sent (and therefore received) on a different thread than the main thread. Only on the main thread will you be able to update UI elements (like a label).

See my answer to this question for some insight into threads and NSNotifications.

Use something like:

NSLog(@"Code executing in Thread %@",[NSThread currentThread] );

to compare your main thread versus where your recieveNotifcation: method is being executed.

If it is the case that you are sending the notification out on a thread that is not the main thread, a solution may be to broadcast your nsnotifications out on the main thread like so:

//Call this to post a notification and are on a background thread      
- (void) postmyNotification{
  [self performSelectorOnMainThread:@selector(helperMethod:) withObject:Nil waitUntilDone:NO];
}

//Do not call this directly if you are running on a background thread.
- (void) helperMethod{
  [[NSNotificationCenter defaultCenter] postNotificationName:@"SOMENAME" object:self];
}

If you only care about the label being updated on the main thread, you can perform that operation on the main thread using something similar to:

dispatch_sync(dispatch_get_main_queue(), ^(void){
                            [label setText:@"Success!"];
                        });

Hope that was helpful!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文