Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UISegmentedControl selected index or value programmatically

I have a UISegmentedControl with two segments (index: 0 and 1) that I am trying to reset programmatically to 0. When I run the command below it does not reset the segment index to 0 as expected. Instead it highlights segment indexed 1.

    [seg setSelectedSegmentIndex:0];

Oddly when I log the selected segment, it returns 0.

    NSLog(@"seg setSelectedSegmentIndex %d", seg.selectedSegmentIndex); 

Also oddly, after running setSelectedSegmentIndex:0 I cannot reselect segment 0 manually by touch, it seems to be locked to segment 1, until I tap 1 wherein it works as normal.

Here is the setup for the button:

    itemArray = [NSMutableArray arrayWithObjects: @"Zero", @"One", nil];
    seg = [[UISegmentedControl alloc] initWithItems:itemArray];
    [seg setFrame:segRect];
    seg.segmentedControlStyle = UISegmentedControlStyleBar;
    seg.momentary = NO;
    [seg addTarget:self action:@selector(someAction:) forControlEvents: UIControlEventValueChanged];
    [mainView addSubview:seg];

NOTE: To restate what I was trying to accomplish with this question: I want to set a UISegmentedControl's selected index via a variable dynamically, get the appropriate visual feedback, and check the selected index in a method, to make something happen depending on the selected index. See my answer below for a solution.

like image 254
Mr Ordinary Avatar asked Jan 24 '13 05:01

Mr Ordinary


5 Answers

Change in viewDidLoad:

[seg setSelectedSegmentIndex:index];

you change use the index value as your wish.

like image 147
Krishnan8190 Avatar answered Oct 29 '22 17:10

Krishnan8190


From code, you can just do seg.selectedSegmentIndex = someDefaultIndex.

Whether you should set it in viewDidLoad: or not depends entirely on the structure of your application. For example, if your app is starting up and loading the view for the first time and needs to set the control to whatever value it had during the previous run of the app, then it definitely makes sense to do it there.

like image 35
NewStack Avatar answered Oct 29 '22 17:10

NewStack


Swift 3.0

segmentedControl.selectedSegmentIndex = #your value#
like image 2
Abhishek Jain Avatar answered Oct 29 '22 19:10

Abhishek Jain


The correct answer is the @arcady bob one at this link. I have posted an updated version for Swift 5, which I quote here:

yourSegmentedControl.selectedSegmentIndex = 0

yourSegmentedControl.sendActions(for: UIControl.Event.valueChanged)

If you use just the first line, the segmented will react accordingly graphically, but your IBAction associated with the segmented control won't be called. In a few words: the second line is like tapping on the segmented control. This is what I needed and what I was missing.

like image 2
novecento88 Avatar answered Oct 29 '22 18:10

novecento88


This helps sort the selected segment, in terms of the visual feedbak, that is after programatically setting the selected segment this will tint the segments properly.

for (int i=0; i<[seg.subviews count]; i++)
{

    if ([[seg.subviews objectAtIndex:i] respondsToSelector:@selector(isSelected)] && [[seg.subviews objectAtIndex:i]isSelected])
    {
        [[seg.subviews objectAtIndex:i] setTintColor:[UIColor grayColor]];
    }
    if ([[seg.subviews objectAtIndex:i] respondsToSelector:@selector(isSelected)] && ![[seg.subviews objectAtIndex:i] isSelected])
    {
        [[seg.subviews objectAtIndex:i] setTintColor:[UIColor blackColor]];
    }
}
like image 1
Mr Ordinary Avatar answered Oct 29 '22 17:10

Mr Ordinary