Propagate programmatic text field value changes to model using Cocoa Bindings

依然范特西╮ 提交于 2019-12-24 00:43:03

问题


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!)



来源:https://stackoverflow.com/questions/8310236/propagate-programmatic-text-field-value-changes-to-model-using-cocoa-bindings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!