views:

143

answers:

1

I've an application that holds a main window with a list of items, and from that window an undetermined number of windows can be opened. Each of those windows can hold several instances of a model object, with those instances listed in a drawer.

I started my project by making the (main window) list of items extend NSDocument and each other window extend NSWindowController. But functionally the main window is used once every blue moon, despite being the window that should pop-up when the users start the application, and the windows that extend NSWindowController are the ones used extensively by the user and also the ones that end up holding my "document".

Because of that I'm now having problems implementing methods such as New, Open and Save - I find myself writing a lot of code the manuals say should be implemented by the super class.

Because I'm at a cross-roads I wonder how I should implement my application. Should I re-factor my main window into a class that extends NSWindowController and launch it from the xib that holds main menu, or should I keep things the way they are and just override newDocument, openDocument, etc in order to get the desired functionality?


Just to help with the mental image, my application works like MSN - I've a main list with several items on it (the contact list on MSN), when I double click on an item I open a window (you open a chat to a user). My app goes one step further by keeping several instances of a model object for each "chat" window and each instance will be accessible by a table in a drawer.

+1  A: 

You would subclass NSDocument in order to handle a type of document. It may be a general type, such as any image, or a specific type, such as PDF, but you need to create an NSDocument subclass to handle the type, since NSDocument itself doesn't know how.

I'm not sure why people subclass NSWindowController. It seems to work well enough as it is.

I've an application that holds a main window with a list of items, and from that window an undetermined number of windows can be opened. Each of those windows can hold several instances of a model object, with those instances listed in a drawer.

I started my project by making the (main window) list of items extend NSDocument and each other window extend NSWindowController.

That's wrong. If anything, your secondary windows are document windows. The primary window is not.

Make a new controller for the primary window. When the user opens an item in that window, tell the document controller to open the relevant file. You probably don't need to subclass NSWindowController for this.

If the items don't correspond to files, then your application is not document-based, and you should not pretend it is: Don't use NSDocument or NSDocumentController at all in this case.

Peter Hosey
They can be files - the user definitely has the option to create, open and save text from the secondary windows so I guess I should make those extend NSDocument.What should the main window, the list of items, extend? I need it to open on application startup so is my idea of making it extend NSWindowController and add it to the MainMenu xib good?
Rui Pacheco
I would make the controller for the primary window an instance of a direct subclass of NSObject. As I said, I don't know why people subclass NSWindowController—owning a window controller or loading and owning the window directly have both worked just fine for me.
Peter Hosey
The benefit of NSWindowController is that it automatically participates in the responder chain, and it handles memory management of top-level NIB objects. All of this can be done on your own, but I don't see why you'd roll your own. I don't think NSWindowController adds any significant overhead.
Barry Wark