2010-11-04 100 views

回答

159

在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,那麼可以肯定的是,slidercontinuous property也設置。另一方面,如果您不希望事件觸發,因爲用戶是sliding,並且只有當他們完成滑動時,纔將​​連續設置爲NO(或在IB中取消選中)。

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]; 

} 
+1

嗨阿查裏雅,歡迎SO,感謝您的回答。你能否編輯你的答案來描述如何解決OP的問題?這裏不鼓勵使用代碼解答。謝謝! – 2016-06-20 04:56:52

4

對於斯威夫特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 
} 
相關問題