"So there are Oliphaunts. But no one at home will ever believe me."
Random header image... Refresh for more!

Category — cocoa

Creating custom subclasses for MyDocument

I learned two things while playing around with Cocoa.

Firstly, in order to change the default MyDocument subclass of NSDocument to something else, say FooDocument, it is not enough to change the class references. Of course the whole application is tied into Interface Builder’s .xib files by magic, so you have to tell MyDocument.xib’s File’s Owner that its new class type is FooDocument. That wasn’t too hard to figure out. However if you do that you’ll get a cryptic error:

The DocumentType type doesn't map to any NSDocumentClass

This is because the class name of the document is part of the application’s configuration file. In other words you have to update your InfoPlist.

The second thing I figured out is that (NSString *)windowNibName isn’t just for looks. I foolishly changed the return value thinking it would programmatically set the name of the document window. No. The application framework sends this message to the NSDocument instance you have configured for your application (see above) to figure out the name of the .xib (or .nib) file it needs to wake up to display your document. Which is nifty, but one of those mysterious interactions that makes Cocoa hard to learn but easy to use.

Is there a flow chart to model the interaction of the run time and .nibs?

February 17, 2010   Comments Off