NSNotificationCenter 和 UIPickerView 的问题
我希望有人能帮助我解决这个问题:
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的选择器中存在语法错误:
@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 theNSNotification *notification
message. Without it, it's a different signature.问题可能是通知是在与主线程不同的线程上发送(并因此接收)的。只有在主线程上您才能更新 UI 元素(如标签)。
请参阅我对 这个问题了解线程和 NSNotifications。
使用类似以下内容:
将主线程与执行 receiveNotifcation: 方法的位置进行比较。
如果您在非主线程的线程上发送通知,则解决方案可能是在主线程上广播您的 nsnotifications,如下所示:
如果您只关心主线程上更新的标签线程,您可以使用类似以下内容在主线程上执行该操作:
希望有帮助!
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:
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:
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:
Hope that was helpful!