我希望有人可以解决我的通知问题。我有一个通知,看起来可以正确设置,但并未按预期交付。我正在开发一个基于文档的应用程序。委托/文档类从保存文件读取通知时发布通知:

[[NSNotificationCenter defaultCenter] postNotificationName:notifyBsplinePolyOpened object:self];

伐木告诉我,每当我打开保存的文档时,就可以达到这一行。

在DrawView类中,我有窗口封闭通知的观察者,并且BsplinePoly文件打开通知:

[[NSNotificationCenter defaultCenter] addObserver:self
                                                          selector:@selector(mainWindowOpen:)
                                                          name:NSWindowDidBecomeMainNotification
                                                          object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                                          selector:@selector(savedBspline:)
                                                          name:notifyBsplinePolyOpened
                                                          object:nil];

- (void)        mainWindowOpen:(NSNotification*) note
{
        NSLog(@"Window opened");
        _mainWindow = [note object];
}

- (void) savedBspline:(NSNotification*) note
{
        NSLog(@"savedBspline called");
        NSLog(@"note is %@", [note name]);
}

行为很奇怪。当我保存并关闭主窗口并重新打开它时,我会收到“打开的窗口”消息,而不是“ savedbspline”消息。如果我打开一个主窗口并打开先前保存的会话,我会收到“打开的窗口”消息和“ savedbspline称为”消息。

我搜索了在线讨论和Apple Devcenter文档,但我没有看到这个问题。

有帮助吗?

解决方案

NSNotification 交货按预期工作(至少没有 NSNotificationQueue参与了)。它们立即交付,而不是在其他线程上,没有延迟,也没有过滤。

我会说,您的某些对象尚未实例保存和关闭窗口时的通知。

为了帮助调试,我建议您在您的应用程序委托中设置一个通知观察者,该委托仅登录所有通知。您可以肯定,所有通知都按预期发送。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top