[英]Case statement to change UILabel depending on UISlider value
So, straight in to it. 因此,直接进入它。 I wish to change the value of a label depending on the position of the UISlider. 我希望根据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". 如果滑块处于位置1且未触碰,则标签应显示“ Regular”(常规),在值2处应显示“ Soy”(大豆),在值3处应显示“杏仁”。
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. 滑块的最小值为1,它代表普通牛奶。 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. 我知道当前设置不会在标签中显示文本,我试图让它首先显示1、2、3值,但无济于事。
Here is a code snippet... As you see, you can use an array of values, in this case you don't need "case" :) 这是一个代码段...如您所见,您可以使用值数组,在这种情况下,您不需要“ 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. 真正的问题是您将UISlider误用作三态开关。 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. 如果需要三态开关,则应使用三态开关,即UISegmentedControl。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.