cocoa - 关于 NSWindowController 和 NSPersistentDocument 核心数据教程的问题

标签 cocoa core-data

NSPersistentDocument Core Data Tutorial 的实现概述部分它说:

  1. One issue with creating the new top-level object in the nib file is that when you use bindings an object retains other objects to which it is bound. This means that bindings must be broken to ensure there are no retain cycles when a document is closed. Moreover, since the nib file the new controller owns contains top level objects and the controller’s class does not inherit from NSWindowController, you need to release the top level objects when the window is closed.

为什么不直接让 Controller 继承自 NSWindowController 呢?这有什么理由行不通吗?或者这只是风格问题?

最佳答案

正如下面所评论的,我确实让它与 NSWindowController 子类一起使用,而且它似乎确实节省了相当多的代码。

这是我的子类 header :

#import <Cocoa/Cocoa.h>

@interface NewAccountSheetController : NSWindowController {
@private
    BOOL isValidForInsert;
    NSManagedObjectContext * managedObjectContext;
    NSObjectController * objectController;
    NSObjectController * targetController;
}

@property (setter=setValidForInsert:) BOOL isValidForInsert;
@property (nonatomic, retain) IBOutlet NSManagedObjectContext * managedObjectContext;
@property (nonatomic, retain) IBOutlet NSObjectController * objectController;
@property (nonatomic, retain) IBOutlet NSObjectController * targetController;

- (void)beginSheetForWindow:(NSWindow *)window;

- (IBAction)endSheet:(id)sender;

@end

这是 Pastebin 中的实现.

我不知道如何描述所需的绑定(bind)等,但如果您熟悉上面的教程,它们应该很容易推断......我认为。 :-)

关于cocoa - 关于 NSWindowController 和 NSPersistentDocument 核心数据教程的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3487804/

相关文章:

xcode - 如何在基于文档的 Mac 应用程序中向我的文档添加自定义文档图标?

macos - 如何知道 NSTableView 何时完成(重新)加载其内容?

swift - UITableView 在滑动删除但不是整个 UI 后卡住

ios - 'NSBatchDeleteRequest' 仅适用于 iOS 9.0 或更新版本

iOS 崩溃与核心数据

multithreading - dispatch_queue 和核心数据

cocoa - 同步显示非模态模板选择器

arrays - 将字符串拆分为数组 string.components(separtedBy : ",") consumes more time

swift - CoreData Predicate 获取包含数组中任何单词的每个句子

objective-c - 保存数组