Cocoa document based app, NSWindowController subclass as “main window”

烂漫一生 提交于 2019-12-05 04:30:15

问题


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:


  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];
    }
    
  2. 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.
    //}
    
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!