基于Cocoa文档的应用程序,NSWindowController子类为"主窗口"

use*_*212 3 macos nsdocument nswindowcontroller

我有一个基于Cocoa文档的应用程序.我希望"主窗口"由我的子类管理NSWindowController.我创建了子类,并将其接口布局在一个同名的.xib文件中.我最终想要像NSDocument管理窗口一样的行为,而是让它由一个管理NSWindowController.

首先,我该怎么办?其次,在采用这种方法时,我需要考虑的是什么特别的事情,例如如何处理打开和保存?

Mar*_*k H 5

  1. 用您自己的windowController实例覆盖makeWindowControllers

    //Lazy instantiation of window controller
    - (WindowController *)controller {
      if (!_controller) {
          _controller = [[WindowController alloc] initWithWindowNibName:@"Document"];
      }
    
      return _controller;
    }
    
    - (void)makeWindowControllers {
      [self addWindowController:self.controller];
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. comment windowNibName&windowControllerDidLoadNib:aController方法

    //- (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.
    //}
    
    Run Code Online (Sandbox Code Playgroud)
  3. 将Document.xib文件所有者类从NSDocument更改为WindowController

XIB重命名

从WindowController中,您可以向文档类发送消息(调用方法).

另外,请确保您了解此图表:

在此输入图像描述