Change the text of UILabels using the value of UISliders

Hi guys, I was wondering how I can show the value of UISliders as the text of UILabels. Thanks

+3
source share
3 answers

Add an action to the slider, for example:

[slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];

Where the method sliderChanged:looks something like this:

- (void)sliderChanged:(UISlider *)slider {
    self.label.text = [NSString stringWithFormat:@"%g", slider.value];
}
+8
source

Try the following:

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

If the label and / or slider are IB elements, define IBOutlets and connect them.

And then connect the slider action sliderChangedto this method.

Good luck

+7
source
//This is for getting the Int Value

- (IBAction)sliderValueChanged:(UISlider *)sender 
{ 
  yourtextlabel.text =  [NSString stringWithFormat:@"%d", (int)yourslideroutletname.value];
NSLog(@"the selider value==%@",yourtextlabel.text);
 }

//This is for getting the float Value

- (IBAction)sliderValueChanged:(UISlider *)sender 
{ 
  yourtextlabel.text =  [NSString stringWithFormat:@"%f", yourslideroutletname.value];
NSLog(@"the selider value==%@",yourtextlabel.text);
 }
+1
source

All Articles