Re: Preferences
Re: Preferences
- Subject: Re: Preferences
- From: Scott Anguish <email@hidden>
- Date: Wed, 7 Apr 2004 19:49:55 -0400
On Apr 7, 2004, at 6:25 PM, Seth Willits wrote:
Is it acceptable to subclass NSApplication and handle things like
showing the preferences window and getting preferences through it from
the rest of the application? I'm thinking of using bindings to connect
everything together.
(This is a rather long email, if you have any comments or questions on
this design pattern, please email me, since this is something I'm
trying to clarify in the doc)
The best design pattern for this is probably to keep the prefs in a
separate class and nib. It means you don't need to load it until you
need to, and you get good encapsulation.
- create a new class called PrefsPanelController, a subclass of
NSWindowController
- create a new nib called PrefsPanel.nib
- import PrefsPanelController into that nib, and set it as the File's
Owner
- connect the window outlet of the files owner to a panel in that
nib, this will be your prefs panel
- in PrefsPanelController
- add a method that sets up the initalValues for the shared
controller, this must be called before ANY nibs that use the
sharedUserDefaultsController are loaded.
- registers the default values with NSUserDefaults using
registerDefaults:
- this might be the same dictionary used with the initialValues:,
(I can explain the situations where it isn't, but usually it should be)
+ (void)setupInitialValues
{
NSString *initialValuesPath;
NSDictionary *initialValuesDict;
// load the initial values for the preferences
initialValuesPath=[[NSBundle mainBundle]
pathForResource:@"InitialValues" ofType:@"plist"];
initialValuesDict=[NSDictionary
dictionaryWithContentsOfFile:initialValuesPath];
// set them in the user default controller.
[[NSUserDefaultsController sharedUserDefaultsController]
setInitialValues:initialValuesDict];
}
make a class method that will return a shared instance of the
PrefsPanelController
+ (PrefsPanelController *)sharedPrefsPanel
{
// create a single static variable to hold the shared prefs panel
instance
static PrefsPanelController *_sharedPrefsPanel = nil;
// have we already created a prefs panel instance?
if (!_sharedPrefsPanel) {
// nope, so make on
_sharedPrefsPanel = [[PrefsPanelController alloc]
initWithWindowNibName:@"PrefsPanel"];
}
// return the shared instance
return _sharedPrefsPanel;
}
you can add these if you need to domore setup after teh nib is loaded
- (void)awakeFromNib
{
NSLog(@"PrefsPanelController got awake from nib");
}
add a method to show the window
- (void)showPreferencesPanel:sender
{
// show me the panel!
[[self window] makeKeyAndOrderFront:self];
}
OK.. so now, in your Application Delegate
+ (void)initialize
{
// insert the initial values into the shared
NSUserDefaultsController
[PrefsPanelController setupInitialValues];
// do anything else
}
and add a method to show the prefs panel.. this you can attach to a menu
- (void)showPreferencesPanel:sender
{
// get the shared preference panel instance
PrefsPanelController *sharedPrefsPanel=[PrefsPanelController
sharedPrefsPanel];
// show it to the user
[sharedPrefsPanel showPreferencesPanel:self];
}
You can then add more outlets to the PrefsPanelController if you need
to. You bind anything you need to in that nib to the
sharedUserDefaultsController.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: | |
| >Preferences (From: Seth Willits <email@hidden>) |