Re: Programatically opening an NSDocument subclass ??
Re: Programatically opening an NSDocument subclass ??
- Subject: Re: Programatically opening an NSDocument subclass ??
- From: Mark Piccirelli <email@hidden>
- Date: Fri, 21 Sep 2001 23:19:50 -0700
Robert --
Several pointers that I hope are helpful:
It would be better if your document-opening loop were to call
-[NSDocumentController openDocumentWithContentsOfFile: display:].
That's the method that -[NSDocumentController openDocument:] calls in
this same situation. It will take care of things like proper handling
of the opening of documents that are already open, and take advantage of
NSDocumentController's file type to document subclass mapping.
Also, NSDocumentController maintains a list of open documents when you
use it like this, and it will do things like handling the quitting-time
"Do you want to save?" UI for all open modified documents.
Your action method should probably present some sort of alert panel if
-openDocumentWithContentsOfFile: display: returns nil.
If you use NSDocumentController as I recommend, it might be most natural
if you either make your action method a method of a NSDocumentController
subclass, or just override -[NSDocumentController openDocument:]. There
are two ways to get Cocoa to use your subclass of NSDocumentController.
See the comment underneath +sharedDocumentController in
NSDocumentController.h.
-- Mark
On Monday, September 17, 2001, at 09:04 AM, Robert Miller wrote:
Thanks for the info. Here is a code snippet of what I've implemented
and it
works OK, Whether it is the optimum solution is still a question.
-(IBAction) openYourDocument:(id)sender
{
int result;
NSOpenPanel *oPanel = [NSOpenPanel openPanel];
[oPanel setAllowsMultipleSelection:YES];
// present the user with an open file dialog that allows multiple file
selection
// note our 'types' array is nil, allowing for all file types to be
selectable
result = [oPanel runModalForDirectory:NSHomeDirectory() file:nil
types:nil];
if (result == NSOKButton)
{
NSArray *filesToOpen = [oPanel filenames];
int count = [filesToOpen count];
int i;
// loop through the files selected and create a YourDocument for each.
for (i = 0; i < count; i++)
{
YourDocument *newDoc =
[[YourDocument alloc]
initWithContentsOfFile:[filesToOpen objectAtIndex: i]
ofType:@"YourDocument"];
// this tells the document to load the nib window
[newDoc makeWindowControllers];
// now display the window
[newDoc showWindows];
}
}
}