问题
I have a Cocoa document based app. I want the "main window" to be managed by my subclass of NSWindowController
. I have created the subclass and laid out its interface in a .xib file with the same name. I ultimately want the same behaviour as if the NSDocument
managed the window, but instead have it managed by an NSWindowController
.
First of all, how do I do it? Second, are the anything special I have to think about when going with this approach, such as how to handle open and save?
回答1:
Override makeWindowControllers with your own windowController instance
//Lazy instantiation of window controller - (WindowController *)controller { if (!_controller) { _controller = [[WindowController alloc] initWithWindowNibName:@"Document"]; } return _controller; } - (void)makeWindowControllers { [self addWindowController:self.controller]; }
comment windowNibName & windowControllerDidLoadNib:aController methods
//- (NSString *)windowNibName //{ // // Override returning the nib file name of the document // // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead. // return @"Document"; //} //- (void)windowControllerDidLoadNib:(NSWindowController *)aController //{ // [super windowControllerDidLoadNib:aController]; // // Add any code here that needs to be executed once the windowController has loaded the document's window. //}
Change Document.xib File Owner Class from NSDocument to your WindowController
From your WindowController you can send a message (call method) to your document class.
Also make sure you understand this diagram:
来源:https://stackoverflow.com/questions/25385088/cocoa-document-based-app-nswindowcontroller-subclass-as-main-window