views:

69

answers:

2

I have the Value Changed event of two UISliders (both of which have referencing outlets) wired up to the following method:

-(IBAction) sliderMoved:(id) sender {}

How can I determine which slider was moved so that I can get its value and update the corresponding label? Or would it be simpler to have two separate events, one for each slider? The second option seems like unnecessary replication to me.

Cheers, Dan

+4  A: 

It's going to be the sender variable. Just do all your work with it.

It's legal to strongly type it, by the way. So if you know you're only going to deal with UISlider objects, you can do -(IBAction)someAction:(UISlider*)slider {}.

zneak
That's great, I guess ID is the equivalent of a C# Object type? The problem still remains, though - is there a pleasant way in which I can determine which one of the two sliders it is that triggered the event?
Daniel I-S
You can use the == operator to compare the pointer of the `sender` object and of your known sliders. As of the `id` type, it's like C#'s `object` type, except the compiler will assume that _all_ method calls on it will succeed (and thus will emit no warnings for any call whatsoever).
zneak
Thanks very much for your help.
Daniel I-S
+1  A: 

You can use [sender tag] to get the tag of the slider if you have set that up. Assign the tag when you create the sliders or in interface builder.

-(IBAction) sliderMoved:(UISlider*)sender {
switch ( [sender tag] ) {
case kMyOneSlider: ... break;
case kMyOtherSlider: ... break;
}
}

You can use == with the outlet members for each slider:

-(IBAction) sliderMoved:(UISlider*)sender {
if ( sender == mOneSlider ) ...;
if ( sender == mOtherSlider ) ...;
}

Or you can set up different actions for each slider. I generally share one action method if there is some common code in the handlers.

drawnonward
Made this the official 'answer' since it answered my question more directly and succinctly.
Daniel I-S