【问题标题】:Cocoa Bindings: NSObjectController not KVC-compliant for the representedObject propertyCocoa 绑定:NSObjectController 不符合 KVC 的代表对象属性
【发布时间】:2009-12-29 00:32:16
【问题描述】:

我浏览了一堆 Core Data 示例和 Apple 文档。经过一整天的工作,我陷入了困境。

我只想在文本字段中输入一些文本,保存文件,再次打开它并在那里查看文本。

我做了一个非常简单的基于 Core Data 文档的应用程序来进行实验。详情如下:

1) 数据模型有一个实体(“Note”)和一个属性(“title”),它是一个 NSString。

2) 我创建了一个视图控制器“ManagingViewController”,它将一个名为“NoteView”的视图加载到 MyDocument.xib 的一个框中,没有问题。 NoteView.nib 里面只有一个 NSTextField。

ManagingViewController.h

#import <Cocoa/Cocoa.h>
#import "Note.h"

@interface ManagingViewController : NSViewController {
NSManagedObjectContext *managedObjectContext;
IBOutlet NSTextField *title;

}
@property (retain) NSManagedObjectContext *managedObjectContext;
@property (retain, readwrite) NSTextField *title;
@end

和ManagingViewController.m

#import "ManagingViewController.h"
#import "Note.h"

@implementation ManagingViewController
@synthesize managedObjectContext;
@synthesize title;

- (id)init
{

    if (![super initWithNibName:@"NoteView" bundle:nil]) {
        return nil;
    }

    return self;

}
@end

我有一个名为“Note.h”的 NSManagedObject

#import <CoreData/CoreData.h>
#import "ManagingViewController.h"
@interface Note :  NSManagedObject  
{
}
@property (nonatomic, retain) NSString * title;
@end

和 .m 文件:

#import "Note.h"
#import "ManagingViewController.h"
@implementation Note 
@dynamic title;
@end

在 NoteView.nib 我的:

1) 文件的所有者是ManagingViewController,IBOutlets 到Text Field 和视图已连接。

2) 我将一个 NSObjectController 对象拖到名为“Note Object Controller”的 Interface Builder 文档窗口中。我将模式设置为“实体”,将实体名称设置为“注释”。 “准备内容”和“可编辑”被选中。 (我已经完成并能够在这里找到的所有示例都使用 NSArrayController。我不需要数组控制器对吗?我确实希望能够为同一个应用程序打开多个窗口,但我仍然不认为我需要数组控制器吗?所有示例都有一个 NSTableView 和一个添加按钮。这里不需要添加按钮,因为我没有 NSTableView)。

3) 值的 NSTextView 绑定我将它绑定到“Note Object Controller”,控制器键为表示对象,模型键路径为标题。

当我运行我的应用程序时,我得到了

[<NSObjectController 0x20004c200> addObserver:<NSTextValueBinder 0x20009eee0>
forKeyPath:@"representedObject.title" options:0x0 context:0x20009f380] was 
sent to an object that is not KVC-compliant for the "representedObject" property.

我做错了什么?我想在文本字段中输入,保存文件,再次打开它并查看那里的文本。

【问题讨论】:

    标签: cocoa core-data nsobject


    【解决方案1】:
    [<NSObjectController 0x20004c200> addObserver:<NSTextValueBinder 0x20009eee0> forKeyPath:@"representedObject.title" options:0x0 context:0x20009f380] was sent to an object that is not KVC-compliant for the "representedObject" property.
    

    我做错了什么?

    错误消息告诉您您做错了什么:您正在尝试绑定到对象控制器的representedObject 属性,但它没有。无法绑定到不存在的属性。

    Note 是 NSObjectController 的内容对象,因此这是您需要绑定到的控制器键:content

    【讨论】:

    • 好的,所以我有许多使用 NSArrayController 和排列对象的 Core Data 教程(没有使用 NSObjectController)。我假设如果我回来并完全理解这些示例中的绑定是如何工作的,然后使用 NSObjectController 和内容应用这些知识,我应该让它工作。如果这是真的,我还有一个问题。 NSArrayController 示例调用 add: 选择器(当在表中创建新行时)。 NSObjectController 是否需要等效项?谢谢
    • 需要注意的关键是,在 IB 中,Controller Key 和 Model Key Path 是分开的。这是有原因的:第一个键(控制器键)访问控制器的某些属性,其值是模型的某些部分;其余的键路径(模型键路径)深入模型以访问更多基本对象,例如字符串和图像以供视图显示。这是 Cocoa 期望并在您的应用程序中强制执行 MVC 分离的众多方式之一。
    • 您应该将add: 作为一个单独的问题提出,因为它与这个问题无关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多