Re: Multiple sliders updating the same text field
Re: Multiple sliders updating the same text field
- Subject: Re: Multiple sliders updating the same text field
- From: "I. Savant" <email@hidden>
- Date: Tue, 24 Jul 2007 11:39:58 -0400
On 7/24/07, Nathaniel Gottlieb-Graham <email@hidden> wrote:
Here's my situation: I have a couple of sliders that both need to
update the same text field. The sliders all send values from -2 to
2, only stopping on integer values (-2, -1, 0, 1, 2). The text field
is supposed to display the combined total of these sliders, so if one
is at -2 and another is at 2, it should display 0. For some reason,
I'm having a lot of trouble with this. Here's the code for one slider:
Maybe I'm missing something, but why not have both sliders call the
same action and just update the text field based on the two sliders'
current values?
- (IBAction)religiousSliderChanged:(id)sender
{
// We ignore sender; we have two specific sliders,
// so we check them implicitly, no matter what was sent
int sliderAValue = [sliderA intValue];
int sliderBValue = [sliderB intValue];
// Update your model here ...
// Now update the field
[textField setIntValue:(sliderAValue + sliderBValue)];
}
Depending on your goals, you might want to consider using Bindings
(with dependent keys). The sliders would be bound to "inputA" key and
"inputB" key, while the text field would be bound to "output" key. The
"output" key would register as a dependent key on "inputA" and
"inputB". That way, any changes to the input keys (via the slider)
would automatically trigger anyone interested in "output" to get its
value. If "output" always returns "inputA plus inputB", you don't have
to worry about keeping track of "output" since it's directly dependent
on the inputs.
Assuming the code above is in an NSDocument subclass, just create
the proper accessor methods in your document (normal ones for the two
inputs, and one that simply returns the desired result for the
output), and call -setKeys:triggerChangeNotificationsForDependentKey:
in your +initialize method. Bind the controls to their respective keys
and you're done.
See the documentation for what constitutes a "proper accessor
method" for use with KVC/KVO (ie, remember -will/didChangeValueForKey:
encapsulation, etc.).
--
I.S.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden