How to connect Model through Controller to View using bindings?

Posted by Paperflyer on Stack Overflow See other posts from Stack Overflow or by Paperflyer
Published on 2010-03-17T13:17:28Z Indexed on 2010/03/17 13:21 UTC
Read the original article Hit count: 162

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about objective-c