Saturday, 15 August 2015

cocoa - How do I post-process a control’s input, and update all attendant controls? -



cocoa - How do I post-process a control’s input, and update all attendant controls? -

i’m working on little optical-illusion app. part of that, have model key (a cgfloat) representing angle.

i have 3 controls — nstextfield, nsstepper, , nsslider — each bound model key. (the nstextfield created “text field number formatter”.)

i want angle fall between -45 , 45 degrees @ times. want rounded nearest integer.

to end, i’ve implemented setangle method applies rules. in add-on rounding input, replaces value falling outside acceptable range closest valid value.

i notice whenever utilize 1 of controls alter angle’s value, other 2 controls reflect post-processing value — submitter not.

for instance, if move slider, text field shows rounded value, not possibly-fractional value slider submitted.

likewise, if come in 44.5 in text field, slider’s position corresponds 45.

however, submitting command still displays “raw” value submitted: text field in lastly illustration continues read 44.5.

placing

[self willchangevalueforkey:@"angle"];

and

[self didchangevalueforkey:@"angle"];

around code in setangle changes value had no effect on this.

my principal questions, then, these:

is there, speaking, “correct” way alter value submitted control, such all controls bound key updated value? i'm not exclusively sure angleset right place pull sort of post-processing shenanigans am, i'm less sure else should so. (the apple docs regarding validation in key-value coding expressly discourage using validation end.)

if there's no general-purpose mechanism setting tweaked version of model key, , notifying all associated controls after fact, there way identify single command doing actual setting, , update post-processed value?

thanks in advance!

use cocoa binding easiest way situation. declare property

@property cgfloat angle;

and bind value of controls keypath angle or someobject.angle (it depends on implementation)

cocoa

No comments:

Post a Comment