/ Published in: Objective C
The UISegmentedControl consists of a horizontal control divided into segments. Segmented controls are useful for allowing users to pick from a group or set of values.
Each segment functions as its own button. By default, selecting a segment will deselect the others in the control (much as a radio button does in HTML). You can alter this behavior by setting the "momentary" property.
Each segment functions as its own button. By default, selecting a segment will deselect the others in the control (much as a radio button does in HTML). You can alter this behavior by setting the "momentary" property.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
- (void)segmentClick:(id)sender { NSLog(@"you selected segment %d",[sender selectedSegmentIndex]); } - (void)viewDidLoad { [super viewDidLoad]; UISegmentedControl *mySegment = [[UISegmentedControl alloc] initWithItems:arrSegments]; CGRect segmentRect = CGRectMake(10,50,300,40); [mySegment setFrame:segmentRect]; [mySegment addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged]; [mySegment setSegmentedControlStyle:UISegmentedControlStyleBar]; [mySegment setTintColor:[UIColor darkGrayColor]]; // mySegment.momentary = YES; // allow multiple multiple selection //select first item [mySegment setSelectedSegmentIndex:0]; //change a segment size [mySegment setWidth:120.0 forSegmentAtIndex:1]; //add a new segment [mySegment insertSegmentWithTitle:@"new" atIndex:2 animated:YES]; //add segment to main view [self.view addSubview:mySegment]; }