Daniel Situnayake
Daniel Situnayake

Reputation: 2904

How do I determine which control fired an event?

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

Upvotes: 0

Views: 156

Answers (2)

drawnonward
drawnonward

Reputation: 53689

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.

Upvotes: 1

zneak
zneak

Reputation: 138261

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 {}.

Upvotes: 4

Related Questions