How to connect Model through Controller to View using bindings?
- by Paperflyer
I have an NSTextField in my view. Its value is bound to an NSNumber *number in my controller. The controller simply calls through to the model (value) to get the appropriate value.
// In the controller
- (NSNumber *)number {
return [NSNumber numberWithFloat:[model value]];
}
- (void)setNumber:(NSNumber *)aNumber {
[model setValue:[aNumber floatValue]];
}
This is fine, only the controller is not notified of model changes and thus, changing the value in the model does not update the NSTextField.
The only other option I can think of is to have the model notify the controller and the controller manually update the view through an Outlet. But this circumvents the binding.
// In the model, after value change
[[NSNotificationCenter defaultCenter] postNotificationName:@"ValueChanged" object:self];
// In the controller, after being notified
- (void)updateView:(NSNotification *)aNotification {
[myTextField setFloatValue:[model value]];
}
Is there a better, binding-aware way to implement this communication?