【发布时间】:2014-03-22 03:44:27
【问题描述】:
简而言之:我将NSTextField 绑定到文件所有者(视图控制器)和representedObject.firstName 的模型键路径,但编辑文本字段不会更改firstName。
这里有更多细节。我有一个简单的程序,除了创建Thing(具有一些属性的简单类)和ThingViewController 的实例之外什么也不做。控制器有一个关联的.xib 和一个简单的用户界面——几个文本字段绑定到Thing 的属性。
@interface Thing : NSObject
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
@property (nonatomic) BOOL someBool;
@end
在应用委托中...
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSView *cv = self.window.contentView;
ThingViewController *vc = [[ThingViewController alloc]
initWithNibName:@"ThingViewController" bundle:nil];
theThing = [Thing new];
theThing.firstName = @"Rob";
vc.representedObject = theThing;
[cv addSubview:vc.view];
}
ThingViewController.xib 很简单:
这是第一个文本字段的绑定:
当我运行时,文本字段确实显示“Rob”,因此它朝那个方向工作,但是当我编辑文本字段时,theThing 的 firstName 属性没有改变。
我做错了什么?
编辑:这是上述代码的压缩项目文件的链接:https://drive.google.com/file/d/0B2NHW8y0ZrBwWjNzbGszaDQzQ1U/edit?usp=sharing
【问题讨论】:
-
试试“不断更新价值?”在带有令牌字段的核心数据上下文中对我来说是必要的。用户的编辑不会立即推送到模型。
-
我试过了,但没有看到行为改变。
标签: cocoa osx-mavericks cocoa-bindings nsviewcontroller