How to run a panel that customizes a new NSDocument
How to run a panel that customizes a new NSDocument
- Subject: How to run a panel that customizes a new NSDocument
- From: Ben Haller <email@hidden>
- Date: Wed, 21 Oct 2009 00:43:04 -0400
Hi all. I've got an NSDocument-based class that can be based upon
a variety of "models". The documents are the same type in any case,
but they are configured differently based upon their model. When the
user chooses New, or when the app is launched, I want to run a panel
for the user to choose their desired model. Once they dismiss the
panel, a document using that model will be created. I.e. very much
like how Interface Builder runs a panel where you choose the sort of
nib you want to make, and then it makes you a new nib of that sort.
The question is how to do this. I see the stuff in NSDocument/
NSDocumentController having to do with "types" but that does not seem
relevant; that would be for an app that could open and save both .txt
and .rtf files, for example. All of my documents are of the same
type, they just have different initial state.
What I have ended up with, after a fair amount of fruitless
searching in the NSDocument docs, is this method in my application
delegate:
- (BOOL)applicationOpenUntitledFile:(NSApplication *)theApplication
{
AKModel *chosenModel = [AKNewModelController runNewModelPanel];
if (chosenModel)
{
AKDocument *newDocument = [[AKDocument alloc]
initWithModel:chosenModel];
[newDocument makeWindowControllers];
[newDocument showWindows];
[[NSDocumentController sharedDocumentController]
addDocument:newDocument];
return YES;
}
return NO;
}
I don't like it very much though. It works, but it hard-codes the
sequence of -makeWindowControllers, then -showWindows, then -
addDocument:, all of which NSDocumentController ought to be doing for
me. I wonder if I need to subclass NSDocumentController instead, but
it isn't really clear to me how to do that to achieve this. I could
override -openUntitledDocumentAndDisplay:error:, and run my "model
chooser" panel inside my override, but how do I get the model the user
chose into the new document if the NSDocumentController creates it for
me? Right now I pass the model in to the document's -initWithModel:
method. I could refactor things so that the document created is
generic and model-less, and then set a model on it after it has
constructed; but that would be highly inconvenient, as it happens,
because the windows the document makes depend upon its model, and the
document's windows get shown by NSDocumentController before it would
return control to me (i.e. inside [super
openUntitledDocumentAndDisplay:error:] returns).
There must be a good, clean way to do this. Anyone?
Ben Haller
Stick Software
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden