Re: [newbie] Handling a preferences window
Re: [newbie] Handling a preferences window
- Subject: Re: [newbie] Handling a preferences window
- From: Jeremy Dronfield <email@hidden>
- Date: Tue, 1 Oct 2002 19:15:45 +0100
On Tuesday, October 1, 2002, at 04:47 pm, James DiPalma wrote:
I continue to respond in this thread because you have a good solution
(arguably better for newbies) for bringing up a preferences panel and I
want your solution to part of this list's archives
I'm sorry - I didn't realise you had this in mind. Part of the reason I
haven't posted a detailed account of my approach to handling a prefs
window is that I was under the impression (cf. the docs quoted in my
last post) that my approach was pretty much off-the-shelf Cocoa (at
least in terms of its general structure). Anyhowsomever, here it is for
what it's worth. This is what I've done in one application (another
example I've got differs only in that the prefs window is run as a sheet
attached to the main window):
PREPARATION
1. Create an empty nib in IB. Name it MyPrefs.nib or something similar.
2. Create a window in the nib file and fill it with controls appropriate
for selecting preferences in your app.
3. Create a subclass of NSWindowController, called MyPrefsController (or
whatever). Provide it with all the actions and outlets needed for
handling the window's controls.
4. Instead of instantiating MyPrefsController, select the File's Owner
instance and change its class to MyPrefsController in the Attributes
inspector.
5. Connect up all the actions and outlets (not forgetting the built-in
"window" outlet). Connect the File's Owner to the window's delegate
outlet.
6. Create files for MyPrefsController.
CODE
7. Put an action method called -showPrefs: in your app delegate (we'll
assume it's called MyController) and connect the Preferences... menu
item to its instance in IB. Code the action method, plus a launch method
and setter/accessor:
=============================================
In MyController.h:
@class MyPrefsController;
@interface MyController : NSObject /*or whatever*/
{
MyPrefsController *prefsController;
}
- (void) setPrefsController:(MyPrefsController *)value;
- (MyPrefsController *) prefsController;
- (IBAction)showPrefs:(id)sender;
- (void)launchPrefs; /*see comment below*/
@end
=============================================
In MyController.m:
#import "MyController.h"
#import "MyPrefsController.h"
@implementation MyController
- (void) setPrefsController:(MyPrefsController *)value
{
prefsController = value;
{
- (MyPrefsController *)prefsController {
return prefsController;
}
- (IBAction)showPrefs:(id)sender
{
if ([self prefsController] == nil) [self launchPrefs];
[prefsController showWindow:self];
}
- (void)launchPrefs
{
prefsController = [[MyPrefsController alloc]
initWithWindowNibName:@"MyPrefs"];
[prefsController setMyController:self];
[self setPrefsController:prefsController];
}
/****-launchPrefs needn't necessarily be a separate method - I've done
it that way because in my app it does some other stuff as well.****/
@end
=============================================
8. Code MyPrefsController:
=============================================
In MyPrefsController.h:
@class MyController;
@interface MyPrefsController : NSWindowController
{
MyController *controller;
}
- (void) setMyController:(MyController *)value;
- (MyController *)controller;
@end
=============================================
In MyPrefsController.m:
#import "MyPrefsController.h"
#import "MyController.h"
@implementation MyPrefsController
- (void)awakeFromNib
{
[self setShouldCascadeWindows:NO];
[self setWindowFrameAutosaveName:@"Prefs Window Frame"];
/*Set initial states of controls*/
}
- (void)dealloc
{
[self setMyController:nil];
[super dealloc];
}
- (void)windowWillClose:(NSNotification *)aNotification
{
/*Save pref settings to defaults, then...*/
[controller setPrefsController:nil];
[self autorelease];
}
- (void)setMyController:(MyController *)value
{
controller = value;
{
- (MyController *)controller {
return controller;
}
/***Other action methods here***/
@end
Note: The setter/accessor methods should be included/excluded based on
the communication you need between the two classes; i.e. if they need to
access each other's instance variables or methods. (I know there's
another world of what's-good-for-newbies debate attached to the use of
setters n' getters. I hope I don't trigger that powder keg with this
post.)
Anyway, I think that's pretty much it - "my" method of bringing up a
prefs window. I'm not guaranteeing that I haven't forgotten any details.
If it's got inherent flaws, I'd be glad to have them pointed out. (I'd
also be interested to know what I'm doing that so unusual.) It's
essentially the model I use for all my peripheral windows. Admittedly,
you could do much the same with an NSObject subclass. I maintain,
though, that for purposes of learning how to get an app to perform its
GUI functions, a window controller provides intuitive conceptual
consistency.
And now an admission: [self setShouldCascadeWindows:NO] isn't useful,
exactly - it's essential, in order for -setWindowFrameAutosaveName: to
work properly. By default it's set to YES, and will prevent your window
opening in the saved position if it's too far to the right or bottom of
the screen. Sorry, suggesting it was "useful" was misleading and
careless.
-Jeremy
========================================
email@hidden // email@hidden
The Alchemy Pages:
- fractious fiction at
http://freespace.virgin.net/jeremy.dronfield
========================================
_______________________________________________
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.