0

I configure bindings like this:

@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];

The text field shows the string “Hello” after launch, then I change it to “World” and hit Return. However, only the object controller’s content is updated:

[controller.content isEqualToString:@"World"]; // YES
         [self.name isEqualToString:@"World"]; // NO — WHY?
         [self.name isEqualToString:@"Hello"]; // YES

So how do I configure the NSObjectController to update the local property that it is bound to?

Vadim
  • 9,383
  • 7
  • 36
  • 58
  • 1
    What if instead of binding to self, you initWithContent:self? – Jacob Gorban Nov 20 '16 at 18:41
  • I think this would lead to retain cycle, but the solution is very close. Could you please verify my answer? Thanks a lot, you really helped! – Vadim Nov 20 '16 at 19:25

1 Answers1

0

Solved! Thanks to Jacob Gorban:

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];
Vadim
  • 9,383
  • 7
  • 36
  • 58