开发者

UISlider sender valuechanged

I'm using multiple UISliders on a form, and开发者_如何学C 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!


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.


Assign a value to the tag property of any UI component in IB and evaluate it using a switch case like so:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜