iPhone控件之UISegmentedControl

 1 - (void)segmentClick:(id)sender
2 {
3 NSLog(@"you selected segment %d",[sender selectedSegmentIndex]);
4 }
5
6 - (void)viewDidLoad {
7
8 [super viewDidLoad];
9
10 NSArray *arrSegments = [[NSArray alloc] initWithObjects:
11 [NSString stringWithString:@"0"],
12 [NSString stringWithString:@"1"],
13 [NSString stringWithString:@"2"],nil];
14
15 UISegmentedControl *mySegment = [[UISegmentedControl alloc] initWithItems:arrSegments];
16
17 CGRect segmentRect = CGRectMake(10,50,300,40);
18 [mySegment setFrame:segmentRect];
19
20 [mySegment addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];
21
22 [mySegment setSegmentedControlStyle:UISegmentedControlStyleBar];
23 [mySegment setTintColor:[UIColor darkGrayColor]];
24
25 //select first item
26 [mySegment setSelectedSegmentIndex:0];
27
28 //change a segment size
29 [mySegment setWidth:120.0 forSegmentAtIndex:1];
30
31 //add a new segment
32 [mySegment insertSegmentWithTitle:@"new" atIndex:2 animated:YES];
33
34 //add segment to main view
35 [self.view addSubview:mySegment];
36
37 [arrSegments release];
38 [mySegment release];
39 }
posted @ 2012-03-13 13:11  FoxBabe  阅读(258)  评论(0编辑  收藏  举报