Search code examples
iosswiftxcodeuislideribaction

Creating duplicate outlets/actions on Xcode


I currently have three separate sliders on my viewcontroller.swift that I would like to all perform the same function, which is to round the slider value to the nearest position. I have already completed the first one, as such:

@IBAction func changePos(_ sender: UISlider) {
    slider.value = roundf(slider.value)
}

I have tried many different ways to add the same function to different UISliders by using slider2, changePos2, etc, but my app either crashes or doesn't perform the function.

How do I make separate UISliders perform the same function within the same viewcontroller?


Solution

  • You can hook all 3 sliders up to the same @IBAction. Just use the sender parameter instead of your slider output in the implementation.

    @IBAction func changePos(_ sender: UISlider) {
        sender.value = sender.value.rounded()
    }