什么是基于多窗口文档的Cocoa应用程序的最佳方法? [英] What is the best approach for a multi window document based Cocoa app?

查看:89
本文介绍了什么是基于多窗口文档的Cocoa应用程序的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序 - 基于文档的Core Data应用程序 - 正在进行第二次迭代,现在需要多个窗口来管理多个模型对象。目前它通过一个窗口和一个控制器管理事件和位置。标准生成的文档类目前作为主窗口的控制器。

My app - a document based Core Data app - is going through a second iteration and now needs multiple windows to manage several model objects. Currently it manages Events and Locations through one window and one controller. The standard generated document class acts as controller for the main window at the moment.

我现在想要一个单独的窗口来管理Locations模型对象。看起来好的设计为每个窗口有一个单独的控制器(NSWindowController),但是我意识到这些控制器将无法访问管理对象上下文,这是访问模型对象所需要的。

I now want a separate window for managing Locations model objects. It seems good design to have a separate controller (NSWindowController) for each window, but then I realised that these controllers will not have access to the Managed Object Context, which is required to access the model objects.

这里最好的方法是什么?

What is the best approach here?

编辑:

href =http://stackoverflow.com/users/458390/ughoavgfhw >解决方案如下:

I followed ughoavgfhw solution as follows:


  • 创建为位置添加了一个新的XIB,并添加了一个数组控制器来加载位置对象。

  • 创建了一个自定义控制器 ManageLocationsController $ c> NSWindowController

  • 在位置XIB中创建了自定义控制器文件所有者

  • 将数组控制器的上下文映射到文件Owner and keyPath document.managedObjectContext

  • Created a new XIB for Locations and added an Array Controller to load Location objects
  • Created a custom controller ManageLocationsController as a subclass of NSWindowController
  • Made the custom controller the File Owner in Locations XIB
  • Mapped the Array Controller's context to File Owner and keyPath document.managedObjectContext

我打开位置窗口:

ManageLocationsController *aController = [[ManageLocationsController alloc] initWithWindowNibName:@"ManageLocations"];
[aController showWindow: self];

这是由EventDocument完成的,这是由XCode生成的默认类。

This is done from EventDocument, which is the default class generated by XCode.

当映射数组控制器时,在keyPath字段中留下了一个圆形黑色感叹号,当我打开位置窗口时,它抛出一个异常,说无法执行没有受管对象的操作。显然不好。我缺少什么?

When mapping the Array Controller, this left a round black exclamation mark in the keyPath field and when I open the Location window it throws an exception saying "cannot perform operation without a managed object". Obviously not good. What am I missing?

推荐答案

使用自定义窗口控制器是最好的方法。窗口控制器可能没有对受管对象上下文的直接访问,但它可以访问该文档。您可以使用 windowController.document.managedObjectContext 或使用键路径 document.managedObjectContext 的绑定以编程方式访问它。如果你想模拟对受管对象上下文的直接访问,你可以创建一个readonly属性,从文档加载它。

Using custom window controllers is the best way to do this. A window controller might not have direct access to the managed object context, but it has access to the document, which does. You can access it programmatically using windowController.document.managedObjectContext or from bindings with the key path document.managedObjectContext. If you want to simulate direct access to the managed object context, you could create a readonly property which loads it from the document.

// header
@property (readonly) NSManagedObjectContext *managedObjectContext;

// implementation
- (NSManagedObjectContext *)managedObjectContext {
    return self.document.managedObjectContext;
}
+ (NSSet *)keyPathsForValuesAffectingManagedObjectContext {
    return [NSSet setWithObject:@"document.managedObjectContext"];
}

keyPathsForValuesAffectingManagedObjectContext 方法用于告诉键值观察系统,任何观察 managedObjectContext 属性的对象,只要它返回的路径发生变化,都应该被通知变化。

The keyPathsForValuesAffectingManagedObjectContext method is used to tell the key-value observing system that any object observing the managedObjectContext property should be notified of changes whenever the paths it returns change.

为了使窗口控制器正常工作,必须使用 addWindowController:将它们添加到文档中。如果在文档打开时创建多个窗口,则应在文档方法中覆盖 makeWindowControllers 以创建窗口控制器,因为这将在正确的时间自动调用。如果您根据请求创建窗口控制器,则可以使用任何方法创建它们,只需确保将它们添加到文档中即可。

In order for the window controllers to work properly, they must be added to the document using addWindowController:. If you are creating multiple windows when the document opens, then you should override makeWindowControllers in your document method to create the window controllers, since this will be called automatically at the right time. If you are creating window controllers upon request, you can make them in whatever method you want, just be sure to add them to the document.

[theDocument addWindowController:myNewController];

对于IB中的小黑色惊叹号,你只需要忽略它。 NSDocument 定义 文档属性 ,但 managedObjectContext 属性由 NSPersistentDocument 子类定义。 IB警告您该物业可能不在那里,但你知道这将是你可以忽略它。

As for the little black exclamation mark in IB, you will just have to ignore that. The document property of NSWindowController is defined with the type NSDocument, but the managedObjectContext property is defined by the NSPersistentDocument subclass. IB is warning you that the property might not be there, but you know it will be so you can just ignore it.

这篇关于什么是基于多窗口文档的Cocoa应用程序的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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