Search code examples
swiftuisegmentedcontrol

How can I connect two segment controls to each other?


two segmented controls

I'm trying to disable the bottom segment control if the user clicks Diesel. The problem is I can't connect two IBActions to each other. Such as:

@IBAction func didSelect(_ control: UISegmentedControl) {
    switch control.selectedSegmentIndex
    {
    case 0:
        isPetrol = true
        isDiesel = false

    case 1:
        isPetrol = false
        isDiesel = true

    default:
        print  ("break")
    }
}

@IBACtion func didSecondSelect (_ control: UISegmentedControl) {
    //something here that when case1 is clicked disables it
    }
}

How can I disable it if the top case 1 is clicked?


Solution

  • You are mixing up IBActions and IBOutlets.

    You should create an IBOutlet that points to the second segmented control and change that from the first one's code.

    @IBOutlet weak var secondSegmentedControl: UISegmentedControl!
    
    @IBAction func didSelect(_ control: UISegmentedControl) {
        [...]
    
        secondSegmentedControl.isEnabled = control.selectedSegmentIndex == 0
    }
    

    For more information about working with IBOutlets, check out this question.