PDA

View Full Version : cocoa document based app with shared windows?




Dr144
Oct 31, 2010, 10:14 PM
Hi All,

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 between different open documents (swapping views as needed). Is this possible? Can anyone point me in the right direction (documentation or examples)?

Thanks!



ritsard
Nov 4, 2010, 12:32 AM
can you elaborate what you mean on shared views?

robbieduncan
Nov 4, 2010, 04:28 AM
When I've wanted to do this in the past (for an inspector say) I create a singleton class that controls the inspector panel. This ensures you only ever get one. That class registers for notifications that will be posted when the active window changes. When it receives one of them it updates with the values from the correct document (you can work that out fairly easily).

Comrade Yeti
Nov 4, 2010, 08:12 AM
Take the 2 'shared' views out of the document and make them singletons. Those two views can talk back and forth with the active document with bindings and notifications.

Look at the Sketch+Accessiblity sample from Apple, the main toolbar controller (SKTToolPaletteController) shows this singleton pattern with notifications when the tool is changed.

To bind to the current active document use 'Application.mainWindow.document.[property]'

Sydde
Nov 4, 2010, 09:25 PM
Of course, you could subclass NSDocumentController, which is a singleton class, with ivars for the shared windows in it. Then, whatever is the delegate for a document window (probably the NSDocument subclass) will have a -windowDidBecomeMain: method in which it either gets the windows from the document controller subclass to change their source values, or the controller subclass itself can handle a change message from the document.

To create a subclass of NSDocumentController at the proper time, either instantiate the subclass in MainMenu.nib, or create it in the -applicationWillFinishLaunching: delegate method. The subclass will replace the NSDocumentController class because it directly inherits the +sharedDocumentController method and returns itself.