Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift handle action on segmented control

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)  
like image 445
TechSavy Avatar asked May 30 '15 10:05

TechSavy


People also ask

When would you use segmented control?

A segmented control is a linear set of segments, each of which functions as a button that can display a different view. Use a segmented control to offer choices that are closely related but mutually exclusive. A tab bar gives people the ability to switch between different subtasks, views, or modes in an app.


1 Answers

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.

like image 129
Arbitur Avatar answered Oct 14 '22 10:10

Arbitur