Swift handle action on segmented control

You can add the same target for multiple events.

So lets say your segmentedControlValueChanged: looks like this:

@objc func segmentedControlValueChanged(_ sender: UISegmentedControl) {
    if sender.selectedSegmentIndex == 0 {
        // value for first index selected here
    }
}

Then you can add targets for more than 1 events to call this function:

segmentedControl.addTarget(self, action: #selector(segmentedControlValueChanged(_:)), for: .valueChanged)
segmentedControl.addTarget(self, action: #selector(segmentedControlValueChanged(_:)), for: .touchUpInside)

Now your function will get called when a value was changed and when the user releases his finger.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)