-->

Propagate programmatic text field value changes to

2019-07-22 17:02发布

问题:

I tried a very simple implementation, like this:

@implementation ScrollingTextField
- (void)scrollWheel:(NSEvent *)event {
    self.doubleValue -= event.scrollingDeltaY;
}
@end

I bound the value of the scrolling text field to some other object. Scrolling now updates the visible text on the text field just fine. However, the bound value does not change.

Why does the bound value not change? Or: How can I make the bound value recognize the change?

回答1:

The bound value doesn't change by Apple's design. To propagate the value to the model yourself after a change, adapt this code:

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

(Thanks @DrummerB for that Apple link!)