PDA

View Full Version : How to pass messages between windows




mpemburn
Jul 9, 2009, 09:32 AM
Hi,

I'm sure that I'm not getting this because I'm thinking about it the wrong way ;-)

I need to alert my main window controller that things have changed when the user makes a change in the preferences pane. Is this possible?


Thanks!

-- Mark



kainjow
Jul 9, 2009, 10:55 AM
Use NSNotifications, or KVO if this is a change in a direct preference value which is bound to NSUserDefaultsController.

mpemburn
Jul 9, 2009, 07:17 PM
Thanks, I'll give it a look!

-- Mark

larkost
Jul 10, 2009, 01:49 PM
You don't have to be using NSUserDefaultsController to use KVO, you just have to make sure to use the get/set convention in your method names, and as long as you inherit from NSObject everything should be setup fine to use KVO.

isharan
Jul 10, 2009, 04:04 PM
Definitely NSNotification.

[[NSNotificationCenter defaultCenter] postNotification:@"MyVariableChanged"];

mpemburn
Jul 13, 2009, 07:43 AM
Once I understood what was required, this was pretty easy to implement. To save you some trouble if you've found this thread while searching for help doing the same thing:

In my main controller, I added the following line to awakeFromNib:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(preferencesChanged) name:@"PreferencesChanged" object:nil];


Since it will be looking for a method called "preferencesChanged", I also added this to the main controller:


- (void) preferencesChanged; {
/*
Code to deal with changed preferences goes here
*/
}


Finally, in my Preferences controller, I added this line to the code that applies changes after the user has made their choices:

[[NSNotificationCenter defaultCenter] postNotificationName:@"PreferencesChanged" object:nil];


So, when the code hits the "postNotificationName" line, it drops a message named "PreferencesChanged" into the Notification dispatch table. The observer (in the main controller) is primed to look for any notifications named "PreferencesChanged", and when one is found, it redirects processing to the preferencesChanged method.

-- Mark