When should an NSDocument be extended and when should I extend an NSWindowController?

I have an application containing the main window with a list of elements, and from this window you can open an indefinite number of windows. Each of these windows can contain several instances of the model object, and those instances that are indicated in the box.

I started my project by making the list of objects (main window) an extension of NSDocument, and each other window extends NSWindowController. But functionally, the main window is used once every blue moon, despite the fact that it is a window that should pop up when users launch the application, and windows that expand the NSWindowController are those that are widely used by the user, as well as those that ultimately hold my "document".

Because of this, I am now having problems with methods such as New, Open, and Save - I find that I am writing a lot of manual code say must be implemented by the superclass.

Because I'm at a crossroads, I wonder how I can implement my application. Should I redefine my main window to a class that extends NSWindowController and start it from xib, which contains the main menu, or should I store things as they are and simply redefine newDocument, openDocument, etc., to get what I want functionality?


Just to help with the mental image, my application works like MSN - I have a main list with several elements on it (contact list in MSN), when I double-click on the element that I open the window (you open the chat for the user). My application goes one step further, saving several instances of the model object for each chat window, and each instance will be accessible using the table in the box.

+4
source share
1 answer

You must subclass NSDocument to handle the document type. It can be a generic type, such as any image or a specific type, such as PDF, but you need to subclass NSDocument to handle this type, since NSDocument itself does not know how to do this.

I'm not sure why people are subclassing NSWindowController. It seems to work quite well as it is.

I have an application containing the main window with a list of elements, and from this window you can open an indefinite number of windows. Each of these windows can contain several instances of the model object, and those instances that are indicated in the box.

I started my project by making the list of objects (main window) an extension of NSDocument, and each other window extends NSWindowController.

It is not right. In any case, your secondary windows are document windows. There is no main window.

Create a new controller for the main window. When a user opens an item in this window, tell the document manager to open the corresponding file. For this, you probably do not need a subclass of NSWindowController.

If the elements do not match the files, then your application is not document-based, and you should not pretend that it is: Do not use NSDocument or NSDocumentController at all in this case.

+1
source

Source: https://habr.com/ru/post/1303416/


All Articles