When should I subclass NSDocumentController for OSX app?

702 views Asked by At

I'm building a document-based app with core data support (so my document is NSPersistentDocument) and I'm wondering if I need to subclass NSDocumentController as well as NSWindowController (for every window in my app since I have multiple).

I went over apple documentation, but haven't found a good explanation on NSDocumentController except that it says that "it is unlikely that you'll need to subclass it".

Can someone give me some hints and scenarios on how this all works?!

Any kind of help is highly appreciated!

1

There are 1 answers

1
willbur1984 On BEST ANSWER

An excellent example of subclassing NSDocumentController (though it doesn't use CoreData) can be found in the TextEdit source code. See their DocumentController class. Specifically, they subclass NSDocumentController to support transient untitled documents. Newly opened documents will take the place of empty untitled documents provided they have not been edited.

Another example use would be changing how opened documents map to NSDocument subclasses within your application. By default, it is one-to-one, but suppose you were developing an IDE in the same vein as Xcode. You might have a project document subclass as well as a file document subclass. If the user opens a file that is already present in an open project, you could force the document to open within the project and its related windows rather than create a new file document and window for it.

Hope that helps!