Sliders Are Not Independent

Hey Everyone!


So I am having an issue where I put two sliders on my GUI in Swift and the second one ends up controlling itself, and the first one. However whenever I move the first slider it only effects itself and not the second one. Does anyone know why this is happening? I am using seperate variables for each action function but I still have this weird interference. Here is my code in the view controller:



class ViewController: UIViewController {

@IBOutlet weak var MidiSlider1: UISlider!

@IBOutlet weak var MidiLabel: UILabel!

@IBAction func MidiValueChange(_ MidiSlider1: UISlider) {

let controllerChange = 0xB0; // hexadecimal equivalent of controller change, channel 0

let controllerNumber = 0x0A; // the assigned controller number for something like panning

midi1.midiSend(status: controllerChange, controlID: controllerNumber, value: Int(MidiSlider1.value));

MidiLabel.text = "\(Int(MidiSlider1.value))"

print(Int(MidiSlider1.value));

}

@IBOutlet weak var MidiSlider2: UISlider!

@IBOutlet weak var MidiLabel2: UILabel!


@IBAction func MidiValueChange2(_ MidiSlider2: UISlider) {

let controllerChange2 = 0xB0;

let controllerNumber2 = 0x0B;

midi1.midiSend(status: controllerChange2, controlID: controllerNumber2, value: Int(MidiSlider2.value));

MidiLabel2.text = "\(Int(MidiSlider2.value))"

print(Int(MidiSlider2.value));

}

}


Thank you for taking the time to review this!


- Greg

Replies

>put two sliders -- second one ends up controlling itself, and the first one.


Did you copy/paste the first one to create the second, or did you drag a new one in each time?

You said:

However whenever I move the first slider it only effects itself and not the second one.


I do not see where in code a change to slide1 would change slider2 ?

Should it be in IBAction ? How ? Where is slide2 referenced here ? I must be missing something.


    @IBAction func MidiValueChange(_ MidiSlider1: UISlider) {
      
        let controllerChange = 0xB0; // hexadecimal equivalent of controller change, channel 0
        let controllerNumber = 0x0A;  // the assigned controller number for something like panning
      
        midi1.midiSend(status: controllerChange, controlID: controllerNumber, value: Int(MidiSlider1.value));
      
        MidiLabel.text = "\(Int(MidiSlider1.value))"
        print(Int(MidiSlider1.value));
    }


To which event is it connected ? valueChanged I suppose.


Finally, naming conventions in Swift require that variables, functions… start with lowercase.

func midiValueChange

midiSlider1: UISlider

@IBOutlet weak var midiSlider1: UISlider!

@IBOutlet weak var midiLabel: UILabel!

Hi Claude31,


The second MidiValueChangeFunction I have references to the second slider. From the code I posted I have two slider action functions. I thought I would need an action function for each individual slider in order for me to call the midisend function I created each time the slider value changes, is there another way I should do this? Also I will change my naming convention too. Thank you for the help


- Greg

Sorry to return so late, I missed your reply. Did you solve the issue ?


Otherwise, you should post the complete code, where we can see the IBActions and IBOutlets for all 3 sliders.