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?


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


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.

Need Your Help