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?
It’s because you’re passing
nilfor theobject:parameter. Pass your preferences window instead, or check[notification object] == yourPrefsWindowin your callback.