我像这样配置绑定:
@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong) NSObjectController *controller;
...
self.controller = [[NSObjectController alloc] initWithContent:nil];
[self.controller bind:NSContentObjectBinding toObject:self
withKeyPath:@"name" options:nil];
[self.controller addObject:@"Hello"];
...
NSTextField *textField = [NSTextField textFieldWithString:nil];
[textField bind:NSValueBinding toObject:self.controller
withKeyPath:@"content" options:nil];
启动后文本字段显示字符串“Hello”,然后我将其更改为“World”并按 Return。然而,只有对象控制器的内容被更新:
[controller.content isEqualToString:@"World"]; // YES
[self.name isEqualToString:@"World"]; // NO — WHY?
[self.name isEqualToString:@"Hello"]; // YES
那么如何配置NSObjectController
来更新它绑定的本地属性呢?