NSNotifications 仅在同一对象内发送
嘿,所以我有一个 NSObject 子类通过通知中心发送消息,并且我将通知发送到对象 nil,但唯一可以接收通知的对象是发送通知的
对象同时发出两个通知(以测试是否是线程问题)
[[NSNotificationCenter defaultCenter] postNotificationName:kWGAskingForAuthToken object:nil];
int status = 123;
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"RAR" forKey:@"Status"];
NSNotification *note = [NSNotification notificationWithName:kWGAskingForAuthToken object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] performSelectorOnMainThread:@selector(postNotification:) withObject:note waitUntilDone:YES];
,我的观察者也很简单,
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMyEvent:)name:kWGAskingForAuthToken object:nil];
它是一个单独对象中的同一个观察者,但没有收到通知
Hey there, so I've got an NSObject subclass sending out a message via the notification center, and I'm sending out my notifications out to the object nil but the only object that can receive notifications is the one sending them
I've got two notifications being sent out at the same time (to test if it is a threading issue)
[[NSNotificationCenter defaultCenter] postNotificationName:kWGAskingForAuthToken object:nil];
int status = 123;
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"RAR" forKey:@"Status"];
NSNotification *note = [NSNotification notificationWithName:kWGAskingForAuthToken object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] performSelectorOnMainThread:@selector(postNotification:) withObject:note waitUntilDone:YES];
and my observers are just as simple
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMyEvent:)name:kWGAskingForAuthToken object:nil];
it's the same observer in a separate object which doesnt recieve the notification
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对我来说似乎是正确的方法。唯一的问题是“kWGAskingForAuthToken 在哪里定义?”是否有可能它在多个地方定义?可能不一样?
This looks like the proper way to me. The only question is "where is kWGAskingForAuthToken defined?" Is it possible it's defined in more than one place? Possibly differently?
很遗憾,但我从未设法找到答案,相反,我只是开始向对象发送消息,而不是在全局范围内使用它们。
It's a shame but I never managed to find an answer to this, instead I just started sending messages to objects instead of using them globally.