Search code examples
swiftuisegmentedcontrol

Two segmented controls using one action


I have two separate segmented controls, both with the same segments. How can I set the second segmented control based on the first segmented control?

If the gallons segment is selected in the top segmented control, I want the bottom segmented control to automatically switch to gallons. Screenshot of the segmented controls


Solution

  • You can use something like:

    segmentedControl1.addTarget(self, action: #selector(changeAction), forControlEvents: .ValueChanged)
    func changeAction() {
        segmentedControl2.selectedSegmentIndex = segmentedControl1.selectedSegmentIndex
    }