0

我像这样配置绑定:

@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来更新它绑定的本地属性呢?

4

1 回答 1

0

解决了!感谢雅各布·戈尔班:

self.name = @"Hello";
self.controller = [[NSObjectController alloc] initWithContent:nil];
[self.controller bind:NSContentObjectBinding toObject:self
          withKeyPath:@"self" options:nil];

...

NSTextField *textField = [NSTextField textFieldWithString:nil];
[textField bind:NSValueBinding toObject:self.controller
    withKeyPath:@"selection.name" options:nil];
于 2016-11-20T19:15:38.797 回答