programatically changing values on a bound NSSlider

  • I have a pair of NSSliders that are bound to SharedUserDefaults

    One represents a maximum value, and one represents a minimum.

    I would like to make sure that the value of the maximum slider is
    always greater than the minimum.

    As well as the bindings, the NSSlider's action points to a method
    that checks the matched NSSlider. If the maximum >= minimum contraint
    is violated, the action method call's

    [otherSlider setDoubleValue:[self doubleValue]];

    This works very nicely. When the maximum is dragged down to meet the
    minimum, the minimum slider tracks with it, so that the constraint is
    not violated, and when the minimum slider is dragged backup to meet
    the maximum, the maximum slider starts tracking it.

    However, calling setDoubleValue on the other slider does not seem to
    update the corresponding bound value in the defaults. I'm not
    entirely surprised by this. I'm hoping that there's some simple
    bindings related method I can call to notify that the slider value
    has changed, but I can't quite work out what it is.

    How can I change the value of the other NSSlider programmatically,
    and have the binding pick it up? I tried playing with
    didChangeValueForKey:, but that didn't seem to do it for me.

    I could presumably just change the value on the defaults, and the
    otherSlider would pick that up, but I'd rather avoid that if possible.
  • On Oct 14, 2006, at 11:12 PM, Martin Redington wrote:

    > How can I change the value of the other NSSlider programmatically,
    > and have the binding pick it up? I tried playing with
    > didChangeValueForKey:, but that didn't seem to do it for me.

    basically, you have to change the value in the model rather than in
    the UI. (Second item in the troubleshooting doc for Cocoa bindings
    here http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaBindings/ind
    ex.html#//apple_ref/doc/uid/10000167i
    )

    the way I'd do it is to bind the range values on the sliders to some
    controller, and have that controller ensure that the values (in the
    controller) are in the right range as they change. then the sliders
    will be updated.
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today