Search code examples
iosswiftbuttoncontrolsuisegmentedcontrol

Use Segmented Control Swift 3


I have searched the web and youtube about segmented control, but I only find examples of how to change to a different view.

My goal is to take the selected value from the segmented control and use it in the action on another button.

I will try to explain what I mean :

@IBAction func segmentetControll(_ sender: UISegmentedControl) {
     value 1
     value 2
     value 3
}

@IBAction func calculateButton(_ sender: Any) {
    if value 1 {
        do this
    }
    else if value 2 {
        Do that
    }
    else if value 3 {
        Do thids
    }

Solution

  • You need to connect it via @IBOutlet to file. Then you can use its index to do whatever you want.

    @IBOutlet var segmentedControl: UISegmentedControl!
    
    @IBAction func calculateButton(_ sender: Any) {
        let index = segmentedControl.selectedSegmentIndex
        //...
    }