Re: Utility window architecture question
Re: Utility window architecture question
- Subject: Re: Utility window architecture question
- From: email@hidden
- Date: Fri, 17 May 2002 10:45:19 -0700
Brock Brandenberg wrote:
|My question is, does anyone have a suggestion for a good code
|architecture for a panel that has its content view swapped out at
|runtime? For example, should I be creating a view controller for each
|custom view that is responsible solely for that view's contents, then
|load and unload each controller and associated nib as appropriate?
That's actually how Apple suggests doing things, and I don't think you're likely to find a better approach. In a document somewhere in the Cocoa documentation (even knowing it's there, I can't always find it), Apple talks about using NSWindowControllers as *view* controllers. The heart of the trick is quite simple: grab the content view, retain it, then set the window to nil. In Cocoa:
@interface ViewController : NSWindowController
{
NSView * controlledView;
}
@end
@implementation ViewController
- (void) dealloc
{
[controlledView release];
}
- (void) convertToViewController
{
controlledView = [[[self window] contentView] retain];
[self setWindow: nil];
}
@end
(If you don't retain the content view, it'll get discarded at the same time as the window.) The controller won't try to reload the window, and all the connections established by the nib file remain intact. If the views have to communicate with each other (unlikely, from the sound of it), you can either tell each view controller what panel contains it, or you can have the controllers send notifications which the panel receives and acts on.
Whether to load and unload, or just load and keep around, would depend on how often the displayed view changes, I'd think. If the view changes rarely, then loading and unloading probably makes sense, since there's little point in keeping the view and its controller around when it's not being used. However, if the view could change often, or on short notice, you might be better off keeping all the possible views and their controllers handy for fast switching. (In fact, if the view *does* change frequently, you might want to wrap all the views up in a tab view with no actual tabs. [IB lets you create such things.])
Glen Fisher
_______________________________________________
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.