NSWindowDidBecomeMainNotification 为所有窗口触发
我有一个带有两个窗口的应用程序 - 一个主窗口和一个可以从菜单栏打开的首选项窗口。我正在尝试实现一个通知,即首选项窗口成为主窗口,以便我可以在打开它时更新它,但是只要打开任何窗口(甚至是不同的窗口),我的通知就会触发。
在我的 PrefsWindowViewController.m awakeFromNib 中,我有:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didBecomeMain:)
name:NSWindowDidBecomeMainNotification
object:nil];
在我的 PrefsWindowViewController.m dealloc 中,我有:
[[NSNotificationCenter defaultCenter] removeObserver:self name: NSWindowDidBecomeMainNotification object:nil];
任何人都可以解释为什么当我的 PrefsWindow 之外的另一个窗口成为主窗口时可能会调用此函数?
I have an app with two windows - a main window, and a preferences window which can be opened from the menubar. I am trying to implement a notification that the preferences window becomes the main window so that I can update it when it is opened, however my notificaton is firing whenever any window opens, even a different window.
inside my PrefsWindowViewController.m awakeFromNib I have:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didBecomeMain:)
name:NSWindowDidBecomeMainNotification
object:nil];
And in my PrefsWindowViewController.m dealloc, I have:
[[NSNotificationCenter defaultCenter] removeObserver:self name: NSWindowDidBecomeMainNotification object:nil];
Can anybody explain why this might get called when a different window besides my PrefsWindow becomes the main window?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您为
object:
参数传递了nil
。请改为传递您的首选项窗口,或在回调中检查[notification object] == yourPrefsWindow
。It's because you're passing
nil
for theobject:
parameter. Pass your preferences window instead, or check[notification object] == yourPrefsWindow
in your callback.