NSDocumentController subclass not instantiated first?
NSDocumentController subclass not instantiated first?
- Subject: NSDocumentController subclass not instantiated first?
- From: Keith Blount <email@hidden>
- Date: Sun, 7 Mar 2010 16:09:37 -0800 (PST)
Hello,
I have an NSDocumentController subclass in my application which should be used as the main doc controller, but it doesn’t always seem to get loaded before NSDocumentController itself and thus isn’t always returned by NSDocumentController’s -sharedDocumentController. The docs are very clear on how to create an NSDocumentController subclass in such a way that it will be used as the shared object:
“To get your application to use your custom subclass of NSDocumentController, you must ensure your subclass is the first instance of NSDocumentController created when the application starts up. There are two ways to do this:
1. Create your subclass in the main nib file.
...
2. Create an instance of your subclass in the applicationWillFinishLaunching: method.
...”
(From: http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/Documents/Tasks/SubclassController.html#//apple_ref/doc/uid/20000953)
I thus created an NSObject in MainMenu.nib and set it to my document controller subclass. But the trouble is that despite being instantiated in MainMenu.nib, my subclass isn’t always being used as the shared document controller object (on some launches it is, on others it isn't).
The reason for this must be that a standard NSDocumentController object is somehow getting created before my subclass is instantiated in MainMenu.nib. NSDocumentController.h clarifies this:
“The first instance of NSDocumentController to be allocated and initialized during application launch is used as the shared document controller.”
(I did also try calling [[MyDocumentControllerSubclass alloc] init] in my app delegate’s -applicationWilFinishLaunching:)
Running some test NSLogs, it certainly seems that NSDocumentController’s -initialize method is called before that of my application delegate - although I’m not entirely sure that is telling or not. I’ve been through my code looking for calls to NSDocumentController, trying to find any calls that could conceivably occur before MainMenu.nib gets initiated, but so far I’m stumped.
Clearly this is something wrong with my project somewhere (a new Xcode document-based project created for testing this worked as expected, with the NSDocumentController subclass instantiated in MainMenu.nib being loaded consistently as the -sharedDocumentController). Somehow, somewhere, a standard NSDocumentController object is being created before my subclass gets instantiated in MainMenu.nib. But how can that be?
So my question is, having looked in the obvious places (my NSApplication subclass, my NSApp delegate, and having searched and looked at all occurrences of NSDocumentController being called in my project), does anybody have any tips on where I might look next? I’m a little fuzzy on what gets called before MainMenu.nib is initialised and created, and couldn’t find the relevant information on this in the docs (although I’m sure it’s there and I’m just missing it, so an RTFM link would be much appreciated in that regard).
And just in case I’m taking the wrong approach entirely, this is why I’m subclassing NSDocumentController: My app has a templates panel - a new project chooser - much like the one in Pages, that should appear whenever all document windows are closed in my app (again, just like the one in Pages). To do this, I have overridden -addDocument: and -removeDocument: in my NSDocumentController subclass to close the templates panel if a document is added and open it if a the last document is removed. This works well - when my document controller gets loaded correctly.
There’s obviously some other factor I’m missing, too, because half of the time my subclass gets loaded fine, the other half it’s only loaded after an instance of NSDocumentController and so the latter becomes the shared object.
Many thanks in advance for any pointers on how NSDocumentController might be created before the subclass in MainMenu.nib.
All the best,
Keith
_______________________________________________
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