71

Does a method get called when a UISlider's value changes? I want to be able to update a value based on the UISliders value, but only when it changes...

Thanks for any help.

Girish
  • 5,094
  • 4
  • 33
  • 53
Michael
  • 959
  • 1
  • 8
  • 12

3 Answers3

172

In IB, you can hook up an IBAction to the Value Changed event or in code add target/action for UIControlEventValueChanged. For example:

[mySlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];

- (IBAction)sliderValueChanged:(UISlider *)sender {
    NSLog(@"slider value = %f", sender.value);
}

Note that if you want the value change events as the user is sliding, then be sure that the slider's continuous property is also set. On the other hand, if you don't want the event to fire as the user is sliding and only when they finish sliding, set continuous to NO (or uncheck in IB).

Sam Spencer
  • 8,158
  • 12
  • 70
  • 130
7

For Swift 3

//There is your slider
@IBOutlet weak var mSlider: UISlider!

//add a target on your slider on your viewDidLoad for example
self.mSlider.addTarget(self, action: #selector(MyClass.onLuminosityChange), for: UIControlEvents.valueChanged)

//callback called when value change
func onLuminosityChange(){
    let value = self.mSlider.value
}
Kevin ABRIOUX
  • 12,949
  • 7
  • 80
  • 78
0
ViewController.h

@property (strong, nonatomic) IBOutlet UIProgressView *prgs;

- (IBAction)slider:(id)sender;

@property (strong, nonatomic) IBOutlet UISlider *slider1;

@property (strong, nonatomic) IBOutlet UILabel *lbl;


ViewController.m

- (IBAction)slider:(id)sender 
{

    self.prgs.progress=self.slider1.value;

    self.lbl.text = [NSString stringWithFormat:@"%f", 
self.slider1.value];

}
taskinoor
  • 43,612
  • 11
  • 111
  • 136
  • 1
    Hi Acharya, welcome to SO and thank you for your answer. Can you please edit your answer to describe how this solves the OP's problem? Code-only answers are discouraged here. Thanks! – Tim Malone Jun 20 '16 at 04:56