2016-08-17 7 views
0

変数tipPercentage、UISliderのアクションtipSliderAction、およびUILabel tipPercentageLabelと宣言しました。
また、clear関数を作成しました。ボタンをタップすると、上記のすべてがデフォルト値にリセットされます。ここ
は、コードは次のとおりです。私はデフォルトに値をリセットする場所
スイスのuisliderの値と変数、ラベルの関連付け

var tipPercentage = 1 

@IBOutlet weak var tipPercentageLabel: UILabel! 

@IBOutlet weak var tipSlider: UISlider! 

@IBAction func tipSliderAction(sender: UISlider) { 
    let currentValue = Int(sender.value) 
    tipPercentageLabel.text = ("\(currentValue)") 
    let percentage = Double(currentValue)/100 
    tipPercentage = percentage 
} 

@IBAction private func clear(sender: UIButton) { 
    tipSlider.value = 10 
    tipPercentageLabel.text = String(tipSlider.value) 
    tipPercentage = tipSlider.value 

clear関数です。私は私の質問は、個々の変数をリセットする代わりに、ちょうど1つをリセットする方法がある、そして他のすべてが自分自身を更新するだろうと思いますか?
例えば、を10に設定した場合、clear関数では、clear関数で設定しないと、tipPercentageLabel.texttipPercentageが更新されます。
ありがとうございます。

答えて

1

これは、UISliderのValueChangedイベントのハンドラを追加することで実行できます。

override func viewDidLoad() 
{ 
    super.viewDidLoad() 
    tipSlider.addTarget(self, action: #selector(ViewController.onValueChanged(_:)), forControlEvents: UIControlEvents.ValueChanged) 
} 

@IBAction func onValueChanged(sender: AnyObject) 
{ 
    tipPercentageLabel.text = String(tipSlider.value) 
    tipPercentage = tipSlider.value 
} 
+0

ありがとうございます。 –

1

これは、UISliderのValueChangedイベントによって実行できます。

UIControlEvents.ValueChangedイベントアクションあなたのクリアボタンアクションで今すぐ

@IBAction func onValueChanged(sender: AnyObject) 
{ 
    tipPercentageLabel.text = String(tipSlider.value) 
    tipPercentage = tipSlider.value 
} 

は、それがすぐにUISliderのにValueChangedアクションを呼び出しますし、あなたはすべての値を再設定したり、デフォルト値にUISlider値を設定します。

+0

ありがとうございます。 –

関連する問題