NSNotifications 仅在同一对象内发送

发布于 2024-09-10 07:20:20 字数 784 浏览 4 评论 0原文

嘿,所以我有一个 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 技术交流群。

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

发布评论

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

评论(2

爱殇璃 2024-09-17 07:20:20

这对我来说似乎是正确的方法。唯一的问题是“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?

不可一世的女人 2024-09-17 07:20:20

很遗憾,但我从未设法找到答案,相反,我只是开始向对象发送消息,而不是在全局范围内使用它们。

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.

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