Return to Snippet

Revision: 58936
at August 10, 2012 04:18 by zopebie

Initial Code
- (void)segmentClick:(id)sender
	NSLog(@"you selected segment %d",[sender selectedSegmentIndex]);

- (void)viewDidLoad {
    [super viewDidLoad];
	NSArray *arrSegments = [[NSArray alloc] initWithObjects:
							[NSString stringWithString:@"0"],
							[NSString stringWithString:@"1"],
							[NSString stringWithString:@"2"],nil];
	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];	


Initial URL

Initial Description
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.

Initial Title
Picking a choice from a group or set of values using UISegmentedControl

Initial Tags

Initial Language
Objective C