当UISlider
的{{1}}更改时,是否会调用方法?我希望能够根据value
值更新值,但只有在更改时才会更新...
感谢您的帮助。
答案 0 :(得分:167)
在IB中,您可以将IBAction
与Value Changed event
或UIControlEventValueChanged
的代码添加目标/操作相关联。例如:
[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];
}