基于可可文档的应用

基于可可文档的应用

本文介绍了基于可可文档的应用,NSWindowController子类为“主窗口".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Cocoa文档的应用程序.我希望主窗口"由我的NSWindowController子类管理.我已经创建了子类,并将其接口布置在具有相同名称的.xib文件中.我最终希望获得与NSDocument管理窗口相同的行为,而由NSWindowController管理该窗口.

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. 使用您自己的windowController实例覆盖makeWindowControllers

  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];
}

  • 评论窗口NibName& windowControllerDidLoadNib:aController方法

  • 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.
    //}
    

  • 将Document.xib文件所有者类从NSDocument更改为WindowController

  • Change Document.xib File Owner Class from NSDocument to your WindowController

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

    From your WindowController you can send a message (call method) to your document class.

    还要确保您了解此图:

    这篇关于基于可可文档的应用,NSWindowController子类为“主窗口".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 09-02 05:56