Re: Customized document creation
Re: Customized document creation
- Subject: Re: Customized document creation
- From: Brian Webster <email@hidden>
- Date: Tue, 3 Jul 2001 14:18:20 -0500
On Monday, July 2, 2001, at 10:13 PM, cocoa-dev-
email@hidden wrote:
Hi,
My document-based app creates documents programmatically, based
on input
(user login info) from a panel. Currently, the panel creates the
document object with:
[[NSDocumentController sharedDocumentController]
openUntitledDocumentOfType:@"MainDocument" display:YES];
, then passes the input data to the document object. Ideally, I'd like
to pass the input data during the creation of the document so that the
document object can authenticate the user login info before
creating the
window controller and, if necessary, abort the document altogether.
Can anyone suggest a good way to do this? Do I need to subclass
NSDocumentController?
I have a similar situation in my app where I need to put up a
panel and get input before creating a document, and I can tell
you what I did, although your needs might vary slightly. I
created a subclass of NSDocumentController and overrode the
newDocument: method. Actually I'm not sure which is the best to
override, newDocument:, openUntitledDocumentOfType:display: or
makeUntitledDocumentOfType:, but I used newDocument: in any
case. When the user selects New... from the file menu, the
method gets called and my implementation puts up a modal panel
that gets some info. Then my code looks like this:
NSDocument *newDocument = [[MyDocumentClass alloc]
initWithCustom
Data:blah];
//You only need this line if you'll have multiple window
controllers, otherwise just override -windowNibName in your
document subclass
[newDocument makeWindowControllers];
[self addDocument:newDocument];
[newDocument showWindows];
If you want to be able to reject the info during init, probably
the easiest way is to just do a [self autorelease] and return
nil from the init method. If you want to differentiate between
different errors, you might want to raise an exception (and then
catch and handle it) instead of just returning nil. Also, in
order for your subclass of NSDocumentController to be used, you
must have an instance of it instantiated in your main nib.
--
Brian Webster
email@hidden
http://www.owlnet.rice.edu/~bwebster