当UISlider值改变时会调用什么?

时间:2010-11-04 01:26:59

标签: objective-c uislider

UISlider的{​​{1}}更改时,是否会调用方法?我希望能够根据value值更新值,但只有在更改时才会更新...

感谢您的帮助。

3 个答案:

答案 0 :(得分:167)

在IB中,您可以将IBActionValue Changed eventUIControlEventValueChanged的代码添加目标/操作相关联。例如:

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

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

请注意,如果您希望值更改事件的用户为sliding,请确保slider的{​​{1}}也已设置。另一方面,如果您不希望事件以用户continuous property触发,并且仅在完成滑动时触发,则将连续设置为NO(或在IB中取消选中)。

答案 1 :(得分:5)

对于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
}

答案 2 :(得分: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];

}