When using Cocoa bindings, changing the value of NSTextField does not programmatically update the model

I have an NSTextField associated with my model. If I programmatically change the contents of a text field, the model is not updated. I know you should update the model instead .

But I'm trying to implement a subclass of NSTextField that recognizes scrolling when the mouse hovers over it to change its numerical value. Obviously, I do not have access to the model from this subclass. So you have suggestions, how can I do this?

SOLUTION (thanks noa):

- (void)scrollWheel:(NSEvent *)theEvent {
    [self setFloatValue:[self floatValue] - [theEvent deltaY]];
    NSDictionary *bindingInfo = [self infoForBinding: NSValueBinding];
    NSObject *boundObject = [bindingInfo valueForKey:NSObservedObjectKey];
    NSString *keyPath = [bindingInfo valueForKey:NSObservedKeyPathKey];
    [boundObject setValue:[NSNumber numberWithFloat:[self floatValue]]
               forKeyPath:keyPath];
}
+5
source share
1 answer

You can use the target action or Cocoa Bindings:

  • : updateValue . . .

  • Cocoa : . , KVO . , .

, , . , , .

, , , , , , .


, "". , , , . :

NSDictionary *bindingInfo = [self infoForBinding:NSValueBinding];
[[bindingInfo valueForKey:NSObservedObjectKey] setValue:self.integerValue
                                             forKeyPath:[bindingInfo valueForKey:NSObservedKeyPathKey]];
+6

All Articles