NSDocumentController如何插入我的基于文档的应用程序? [英] How does NSDocumentController slot into my document based app?

查看:269
本文介绍了NSDocumentController如何插入我的基于文档的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建我的第一个严重的Cocoa应用程序。它是一个基于文档的应用程序使用Core Data持久存储。使用Xcode的新项目模板选项,我的项目开始与几个默认类:

I'm building my first serious Cocoa app. It is a document based app using Core Data for persistent storage. Using the Xcode "New project" template options, my project started with a couple of default classes:


  • MainMenu.xib我的应用程式)

  • EventDocument.xib(我应用程式的主视窗)

  • EventDocument.h和EventDocument.m / li>
  • MainMenu.xib (the main menu of my app)
  • EventDocument.xib (the main window of my app)
  • EventDocument.h and EventDocument.m (the persistent document class)

我从那里采用了设计,所有的工作都很好,但是通过另一个问题我遇到了一个Cocoa类NSDocumentController,我不知道。这个类显然提供了在基于文档的应用程序中管理文档类的默认逻辑。

I took the design from there and it all works rather nicely, however through another question I came across a Cocoa class NSDocumentController which I was not aware of. This class apparently offers default logic for managing document classes in a document based application.

我的问题是这个类如何插入我的应用程序 - 如果有 - 需要它?在这一刻,我使用EventDocument类作为一种顶级控制器。它从菜单中选择用户选项(树视图,选项为iTunes),调出不同的视图/控制器来处理这些选项并传递管理对象上下文。如果NSDocumentController是走的路,为什么苹果不生成它作为项目模板的一部分

My question is how does this class slot into my app - if at all - and do I need it? At this moment, I use the EventDocument class as a sort of top level controller. It picks up the user choices from the menu (a tree view with options a la iTunes), brings up the different views/controllers that handle these options and passes around the Managed Object Context. If NSDocumentController is "the way to go", why does Apple not generate it as part of the project template??

推荐答案

相信你误解了NSDocumentController的目标,可能是因为它的名字类似于NSWindowController和NSViewController。

I believe you’re misunderstanding the goal of NSDocumentController, probably because its name is similar to NSWindowController and NSViewController.

在Cocoa MVC中,控制器调解视图和模型。当处理窗口时,控制器通常是NSWindowController的子类,在视图的情况下是NSViewController的子类。

In Cocoa MVC, a controller mediates a view and a model. When dealing with windows, it’s common for the controller to be a subclass of NSWindowController and, in the case of views, a subclass of NSViewController.

在基于文档的体系结构,NSDocument类是表示文档的模型与对应的视图和控制器之间的中介。本质上,它负责基于外部表示重建模型,并提供一些附加与模型和视图相对应的控制器行为的方式。两种设计通常用于:

In the document-based architecture, an NSDocument class is a mediator between the model that represents the document and the corresponding views and controllers. Essentially, it is responsible for recreating the model based on an external representation and providing some way of attaching controller behaviour corresponding to the model and the view. Two designs are commonly used for that:


  • NSDocument子类有效地充当一个窗口例如,通过实现IBActions。这应该适用于简单的应用程序,但它可以迅速导致一个a肿的NSDocument子类处理超过它应该。 文档说:


默认的基于Document的应用程序项目模板不会对NSWindowController进行子类化。如果你正在编写一个简单的应用程序,你不需要子类化NSWindowController。但是,如果您正在编写具有更高级要求的应用程序,您几乎肯定会想要这样做。

The default Document-based Application project template does not subclass NSWindowController. You do not need to subclass NSWindowController if you are writing a simple application. However, if you are writing an application with more advanced requirements, you will almost certainly want to do so.


  • NSDocument子类创建自定义窗口控制器,从而实现控制器行为, p>


  • 在许多(大多数)情况下,不需要NSDocumentController子类 - 应用程序的控制器部分将位于窗口控制器,视图控制器或您的NSDocument子类。也就是说,在某些情况下,它可能是必要的,如文档

    In many (most?) cases there is no need for an NSDocumentController subclass — the controller part of your application would be inside window controllers, view controllers, or your NSDocument subclass. That said, there are some situations where it might be necessary as explained in the documentation:


    通常,您不需要子类NSDocumentController。几乎任何可以通过子类化完成的任务都可以通过应用程序的委托轻松完成。但是,如果需要,可以子类化NSDocumentController。

    Usually, you should not need to subclass NSDocumentController. Almost anything that can be done by subclassing can be done just as easily by the application’s delegate. However, it is possible to subclass NSDocumentController if you need to.

    例如,如果你需要自定义Open面板,显然需要一个NSDocumentController子类。您可以覆盖NSDocumentController方法runModalOpenPanel:forTypes:自定义面板或添加附件视图。

    For example, if you need to customize the Open panel, an NSDocumentController subclass is clearly needed. You can override the NSDocumentController method runModalOpenPanel:forTypes: to customize the panel or add an accessory view. The addDocument: and removeDocument: methods are provided for subclassers that want to know when documents are opened or closed.

    这篇关于NSDocumentController如何插入我的基于文档的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆