process a control’s input, and update all attendant controls?
I'm working on a little optical-illusion app. As part of that, I have a model key (a CGFloat) representing an angle.
I have three controls — an NSTextField
, an NSStepper
, and an NSSlider
— each bound to that model key. (The NSTextField
was created as a “Text Field with Number Formatter”.)
I want that angle to fall between -45 and 45 degrees at all times. I also want it rounded to the nearest integer.
To that end, I've implemented a setAngle
method that applies those rules. In addition to rounding its input, it replaces any value falling outside the acceptable range with the closest valid value.
I notice that whenever I use one of the controls to change the angle's value, the other two controls reflect the post-processing value — but the submitter itself does not.
For instance, if I move the slider, the text field shows the rounded value, not the possibly-fractional value that the slider submitted.
Likewise, if I enter 44.5 in the text field, the slider's position corresponds to 45.
However, the submitting control still displays the “raw” value it submitted: the text field in the last example continues to read 44.5.
Placing
[self willChangeValueForKey:@"angle"];
and
[self didChangeValueForKey:@"angle"];
around the code in setAngle
that actually changes the value had no effect on this.
My principal questions, then, are these:
Is there, generally speaking, a “correct” way to alter a value submitted by a control, such that all controls bound to the key get the updated value? I'm not entirely sure that angleSet is the right place to pull the sort of post-processing shenanigans I am, but I'm even less sure where else I should do so. (The Apple docs regarding validation in key-value coding expressly discourage using validation to this end.)
If there's no general-purpose mechanism for setting a tweaked version of model key, and notifying all associated controls after the fact, is there a way to identify the single control doing the actual setting, and update it with the post-processed value?
Thanks in advance!
use cocoa binding is the most easiest way for this situation. declare a property
@property CGFloat angle;
and bind it to value of the controls with keypath angle or someobject.angle (it depends on your implementation)
链接地址: http://www.djcxy.com/p/14932.html上一篇: 有没有办法在.ASPX页面中注释掉标记?
下一篇: 处理控件的输入,并更新所有伴随控件?