How can I switch the UIsegmentedControll programmatically?

How can I switch the UIsegmentedControll programmatically?

+44
iphone uisegmentedcontrol
Mar 11 2018-11-11T00:
source share
5 answers

The selectedSegmentIndex property identifies the selected UISegmentedControl segment. Set this property to any valid segment index or UISegmentedControlNoSegment (-1) to disable the current selection.

// select the first segment segmented.selectedSegmentIndex = 0; // turn off the current selection segmented.selectedSegmentIndex = UISegmentedControlNoSegment; 
+76
Mar 11 2018-11-11T00:
source share

Alternatively, after you change the call to selectedSegmentIndex 'sendActionsForControlEvents:' for example

 segmentedControl.selectedSegmentIndex = 0 [segmentedControl sendActionsForControlEvents:UIControlEventValueChanged]; 
+58
Mar 04 '13 at 0:17
source share

@jmstone answer, true, this action will not raise the valueChanged event for this control.

One way to overcome this is to simply call the function yourself:

 segmentedControl.selectedSegmentIndex = 3; [self valueChangedMethod:segmentedControl]; 

This will cause:

 - (void)valueChangedMethod:(UISegmentedControl *)segmentedControl { //continue your code here... } 
+22
Nov 28
source share

In Swift:

 segmentedControl.selectedSegmentIndex = 1 

Swift 2:

 segmentedControl.sendActionsForControlEvents(UIControlEvents.ValueChanged) 

Swift 3:

 segmentedControl.sendActions(for: UIControlEvents.valueChanged) 
+12
Nov 05 '15 at 22:37
source share

I had a similar problem when the segmented control did not change. I called "selectedSegmentIndex" etc. Too early. As soon as I called him after calling "viewDidLoad", everything was fine.

+2
Apr 10 '13 at 4:32
source share



All Articles