开发者

When should I extend NSDocument and when should I extend NSWindowController?

I've an application that ho开发者_开发技巧lds 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜