So, straight in to it. I wish to change the value of a label depending on the position of the UISlider. If the slider is in position 1, and untouched, the label should show "Regular", at value 2 it should show "Soy" and value 3, "Almond".
In case you hadn't guessed already, it is to make a milk choice.
Ideally I wish to use a switch/case. The slider has a minimum value of 1 which refers to regular milk. The aim for the app is to use the cloud down the line to save order information, so I am open as to which would be the best solution complying to this future requirement.
Thanks!
Currently....
@IBOutlet weak var sugarValue: UILabel!
@IBOutlet weak var sugarStepper: UIStepper!
@IBOutlet weak var milkChoice: UILabel!
@IBOutlet weak var milkChoiceSlider: UISlider!
@IBAction func sugarChange(sender: UIStepper) {
sugarValue.text = Double(sugarStepper.value).description
}
@IBAction func milkChoiceChange(sender: UISlider) {
milkChoice.text = String(milkChoiceSlider.value)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
I'm aware that the current set up would not show the text in the label, I was trying to just get it to show the 1, 2, 3 values first but to no avail.
Here is a code snippet... As you see, you can use an array of values, in this case you don't need "case" :)
import UIKit
class ViewController: UIViewController {
@IBOutlet var milkChoiceLabel: UILabel!
@IBOutlet var milkChoiceSlider: UISlider!
@IBOutlet var sugarValue: UILabel!
var labelValues: [String] = ["Regular", "Soy", "Almond"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func sliderValueChanged(sender: UISlider) {
sender.value = roundf(sender.value)
let value = Int(sender.value)
sugarValue.text = value.description
// switch (sender.value) {
// case 1:
// milkChoiceLabel.text = labelValues[0]
// break
// case 2:
// milkChoiceLabel.text = labelValues[1]
// break
// case 3:
// milkChoiceLabel.text = labelValues[2]
// break
// default:
// break
// }
// or better ...
milkChoiceLabel.text = labelValues[value - 1];
}
}
The real problem here is that you're misusing a UISlider as a tri-state switch. A slider is continuous ; it's not a switch. If you wanted a tri-state switch, you should use a tri-state switch, namely a UISegmentedControl.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.