macoscocoansdocumentnswindowcontroller

Keeping and NSDocument open without any windows


I have a document-based application which has two document types. The first NSDocument subclass is the "main" document (mainDoc) and it's .xib file is the main editing window for the application. The second NSDocument class is a data source file (dataDoc), a set of these is kept in the user's Application Support folder and the set (or a subset) is opened to be used by mainDoc windows. The dataDoc files can be edited and this is done in their own window from their own .xib file.

Opening dataDocs without making their windows is straight forward with openDocumentWithContentsOfURL:display:completionHandler: however, keeping them open after they have been edited (i.e their only NSWindowController is closed) is not. I created an NSWindowController subclass thinking this would help. However overriding shouldCloseDocument does not help because the document is closed regardless of my wishes if it's last NSWindowController is closed (as specified in the Apple documentation).

How can a close I document's windows but keep the document open? Have I missed something obvious?

One possibility I have though of is to add the mainDoc's windowcontroller to the windowcontrollers for all of the open dataDocs. Is this the best solution? It seems inelegant and requires a bit of stuffing around.

Another possibility is to reopen the dataDoc after it is closed each time, but that is even less elegant and I see it as a last resort.

Thank you for any help!


Solution

  • Looking through the NSDocument documentation, the easiest solution to me seems to be to add an empty NSWindowController to each of these NSDocuments. I’d try having an NSWindowController that didn’t have a NIB, for instance, and when it was asked to bring its window to front it just returned “sure” and didn’t do anything.