Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mpemburn

macrumors member
Original poster
Jan 11, 2008
51
0
Bel Air, MD USA
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

Moderator emeritus
Jun 15, 2000
7,958
7
Use NSNotifications, or KVO if this is a change in a direct preference value which is bound to NSUserDefaultsController.
 

larkost

macrumors 6502a
Oct 13, 2007
534
1
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

macrumors member
Jun 3, 2009
52
0
Definitely NSNotification.

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

mpemburn

macrumors member
Original poster
Jan 11, 2008
51
0
Bel Air, MD USA
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:

Code:
[[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:

Code:
- (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:

Code:
[[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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.