Question

Trying to set the selected side of a UISegmentControl, but nothing happens.

In my viewDidLoad or viewDidAppear this code will not select a segment?

[self.segmentedControl setSelectedSegmentIndex:[@1 integerValue]];

Nothing happens?

Was it helpful?

Solution

I deleted the UISegmentControl from the Storyboard and added it programmatically and it works now. Very weird.

self.segControl.frame = CGRectMake(35, 100, 200, 50);
self.segControl.segmentedControlStyle = UISegmentedControlStylePlain;
self.segControl.selectedSegmentIndex = 1;

[self.view addSubview:self.segControl];

OTHER TIPS

You can use code for adding and setting selected tab in UISegmentControl

 NSArray *itemArray = [NSArray arrayWithObjects: @"Top Twenty", @"Dow thirty", @"Favorite", nil];
    segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
    segmentedControl.frame = CGRectMake(15, 80, 300, 30);
    segmentedControl.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [segmentedControl addTarget:self action:@selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged];
    segmentedControl.selectedSegmentIndex = 0; //**this line represent selected tab**
    [self.view addSubview:segmentedControl];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top