UISlider Sender ID Value

I use several UISliders in the form and I only need one method to track slider changes.

Now I have a method:

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

But since I use several sliders, I want to use the switch statement to run for a specific slider, for example, if I have 2 sliders, and both of them use the above ValueChanged method, I need 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!

+3
source share
2 answers

You just need to compare the parameter senderwith the sliders to find out which one has been moved:

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

IB -setTarget.

+10

IB , :

typedef enum Commands {
    CMD_PLAY_MUSIC = 0,
    CMD_STOP_MUSIC,
    CMD_PAUSE_MUSIC,
    CMD_EDIT_PLAYLIST,
    CMD_ADJUST_VOLUME,
} Commands;


-(id) processUIEvent:(id) sender {

    int tag = [sender tag];
    switch (tag) {
        case CMD_PLAY_MUSIC:   
            // Code here...
            break;

        case CMD_PAUSE_MUSIC:
            // Code here...                
            break;

        case CMD_EDIT_PLAYLIST:
            // Code here...
            break;

        default:
            // Event not handled
            break;
    }
}
0

Source: https://habr.com/ru/post/1759946/