On 21-Oct-09, at 12:57 AM, Graham Cox wrote:

On 21/10/2009, at 3:43 PM, Ben Haller wrote:

There must be a good, clean way to do this.  Anyone?

Well, the expected way is to have different types for your documents. You can still map them all to the same class, and discriminate in the -initWithType:error: method to set up the appropriate model.

However, if you really can't or don't want to do that, I think what you've done is right. Because you have a special designated initializer, you have no option but to manually make the controllers and add the document to the controller.

OK, I've switched over to an NSDocumentController and using different types for my different models. That turned out to be a forced move, because NSApplication's delegate method - applicationOpenUntitledFile: does not get called when the user chooses New from the File menu; that seems to go right to NSDocumentController. -applicationOpenUntitledFile: only seems to get called for the new document created when an app is launched or brought front without an open document. I could have started changing the actions for menu items and so forth, but I decided that since my app is document-based, I ought to go with the document architecture. So I now have an NSDocumentController subclass, and I override openUntitledDocumentAndDisplay:error:. That seems to be in the code path for all kinds of new documents. However, I still find this architecture quite unsatisfying. My - openUntitledDocumentAndDisplay:error: subclass still needs to do way too much work:

- (id)openUntitledDocumentAndDisplay:(BOOL)displayDocument error: (NSError **)outError
{
        NSString *chosenModelName = [AKNewModelController runNewModelPanel];
        
        if (chosenModelName)
        {
                NSError *error = nil;
AKDocument *document = [self makeUntitledDocumentOfType:chosenModelName error:&error];
                
                if (error)
                {
                        if (outError)
                                *outError = error;
                        return nil;
                }
                
                [self addDocument:document];
                
                if (displayDocument)
                {
                        [document makeWindowControllers];
                        [document showWindows];
                }
                
                return document;
        }
        
        return nil;
}

The sequence of -makeUntitledDocumentOfType:error:, then makeWindowControllers, then showWindows, is already in super, but I don't have any way to make super do it. So I have to duplicate that sequence in my own code, which makes my code vulnerable to architectural changes in NSDocument. I gather this sequence has already changed at least once in the past, so it is clearly fragile. There really must be a better way to do this; I feel like I'm running at counter purposes to the design of the framework. Anyone have any better suggestions? I'm curious as to why -makeUntitledDocumentOfType:error: doesn't do the work of adding the document, setting up its windowController,s and showing the windows. That would seem like the natural design to me, rather than making everyone that calls that method do that work themselves. Is there a reason for this design?

Ben Haller
Stick Software

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to