views:

60

answers:

1

Hi,

I'm using multiple UISliders on a form, and I want just one method to keep track of slider changes.

Now I have a method:

- (IBAction) slider1ValueChanged:(UISlider *)sender {  
    somelabel.text = [NSString stringWithFormat:@" %.1f", [sender value]];  
}

But because I use multiple sliders, I want to use a switch statement to fire for a particular slider, so for example if I have 2 sliders, and they both use the above ValueChanged method, I want something like:

    - (IBAction) slider1ValueChanged:(UISlider *)sender {
switch(SLIDERID)
case SLIDER1:
      blabla;
      break;
case SLIDER2:
      update other label;
      break;
case default:
      break;
            somelabel.text = [NSString stringWithFormat:@" %.1f", [sender value]];  
    }

Who can help me?

Thanks in advance!

+2  A: 

You just need to compare the sender parameter to your sliders to figure out which one moved:

-(IBAction) sliderValueChanged:(UISlider*)sender {
    if (sender == self.temperatureSlider) {
        // ...
    } else if (sender == self.altitudeSlider) {
        // ...
    } // etc
}

Then hook all of your sliders to this action in IB or via -setTarget.

Seamus Campbell
Ow my god.. Thanks... I'm so tired I was thinking way too complicated!! Thanks, this solved it!!
CyberK