Swift handle action on segmented control

Poonam picture Poonam · May 30, 2015 · Viewed 44.6k times · Source

I have a HMSegmentedControl with 4 segments. When it is selected, it should pop up view. And when the pop up dismissed, and trying to click on same segment index it should again show the pop up. By using following does not have any action on click of same segment index after pop up dissmissed.

segmetedControl.addTarget(self, action: "segmentedControlValueChanged:", forControlEvents: UIControlEvents.ValueChanged) 

Answer

Arbitur picture Arbitur · May 30, 2015

You can add the same target for multiple events.

So lets say your segmentedControlValueChanged: looks like this:

func segmentedControlValueChanged(segment: UISegmentedControl) {
    if segment.selectedSegmentIndex == 0 {
    }
    ...
}

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

segmentedControl.addTarget(self, action: "segmentedControlValueChanged:", forControlEvents:.ValueChanged)
segmentedControl.addTarget(self, action: "segmentedControlValueChanged:", forControlEvents:.TouchUpInside)

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