I have 5 sliders that reference the same @IBAction func sliderDidSlide(). Each UISlider has a tag 0...4. How do I get the value of each individual slider? I wanted to use sender.tag to get which slider, then use .value to get its value. I've tried sender.tag.value, but that doesn't work.
Below is what I want to do but it doesn't work. I've tried searching but couldn't find a solution. I know I could just have multiple @IBActions, but if possible I'd like to save code and use tags to put them all in the same @IBAction. Any help would be greatly appreciated. Thank you.
@IBAction func sliderDidSlide(_ sender: UISlider) {
let maxPrice = 10000
switch sender.tag {
case 0:
let price = maxPrice * sender.tag.value // this doesn't work
label1.text = "\(price)"
case 1:
// same idea as case 0
case 2:
<#code#>
case 3:
<#code#>
case 4:
<#code#>
default:
break
}
}
sliderDidSlide
will be called whenever one of the sliders' value changes. In each of those calls, the corresponding UISlider
will be passed into the sender
argument label.
You are already telling apart the sliders via the switch sender.tag {
. sender.tag.value
has no meaning, and won't compile, because tag
does not have a property called value
.
Once you've switched over the tags, you can directly access sender.value
.
let price = maxPrice * sender.value /// directly access value
switch sender.tag {
case 0:
label1.text = "\(price)"
case 1:
label2.text = "\(price)
case 2:
label3.text = "\(price)
case 3:
label4.text = "\(price)
case 4:
label5.text = "\(price)
default:
break
}