cocoa document based application with shared windows?
I'm developing a document based app. Each document has three windows (and hence three window controllers). I'd like to set it up so that two of the three windows are shared betw开发者_StackOverflow社区een different open documents (swapping views as needed). Is this possible? Can anyone point me in the right direction (documentation or examples)?
Thanks!
In that case, these shared window controllers should not be owned by any document (since each document would then have its own pair of the “shared” windows), but should be independent, probably owned by the app delegate or the document controller. You may also want to make the windows panels, as an Inspector would be.
You'll want to have each controller track which window is main, and update its window accordingly when the main window changes, because the new main window may have a different document.
Pretty much any tutorial on how to make an Inspector window will help you here.
It looks like you need to override -makeWindowControllers
in your NSDocument
subclass to create the controllers you want, invoking -addWindowController:
on the NSDocument subclass to add your shared window controllers.
I haven't yet had to do this, but those are the methods I'd be looking at.
From Apple's NSDocument class reference:
makeWindowControllers
Subclasses may override this method to create the initial window controller(s) for the document.
- (void)makeWindowControllers
Discussion
The base class implementation creates an NSWindowController object with windowNibName and with the document as the file’s owner if windowNibName returns a name. If you override this method to create your own window controllers, be sure to use addWindowController: to add them to the document after creating them.
This method is called by the NSDocumentController open... methods, but you might want to call it directly in some circumstances.
It is possible, but it'll take a non-trivial amount of work from your side. In summary here's what you need to do:
- Override
setDocument:
in the window controller and maintain the associations that it has to each document. - Make sure that each window controller (
NSWindowController
) disassociates itself from the document before the window is closed. The same goes for each view controllers that may be handling views inside the window. - Subclass the document controller (
NSDocumentController
) and take care of document closing to make sure that multi-document windows are detached from documents before any of the documents are closed.NSDocumentController
is a singleton and thus you need to add an instance in yourMainMenu.xib
file to replace the default one.
You can read my step-by-step guide how to add support for multi-document window controllers here.
精彩评论