Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Segment in UISegmentControl programmatically?

I'm setting UISegmentControl programmatically in my iPhone app. By default it has 2 segment. In my code I'm populating more than two segments. How do I set this, any help?

Update

My question is how do I put more than 2 tabs on segmentController by code?

like image 538
HardCode Avatar asked Nov 30 '22 03:11

HardCode


2 Answers

First of all segmented control in iOS is of UISegmentedControl class, not NS...

To create it with any number of segments you want you can use initWithItems: initialize method - pass array of titles (NSStrings) or images for each segments. e.g.:

UISegmentedControl *segControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil]];

Later you can change you control using insertSegmentWithImage:atIndex:animated:, insertSegmentWithTitle:atIndex:animated: or/and removeSegmentAtIndex:animated: methods.

You can find descriptions on those (and some more!) methods in apple docs.

like image 148
Vladimir Avatar answered Dec 09 '22 15:12

Vladimir


Before your edit, you were actually talking about UISegmentedControl and to set the selected one programatically, you want to use the selectedSegmentIndex property (the documentation for which I've linked for you).

And to add in additional segments, you can use insertSegmentWithTitle:atIndex:animated:.

like image 38
Michael Dautermann Avatar answered Dec 09 '22 15:12

Michael Dautermann