UISegmentControl的使用

- (void)viewDidLoad
{    [super viewDidLoad];   
 //数组要提前建好,里面可以保存一会儿显示出来的文字或者图片   
 //这个文字的长度随意,一会儿segment的每一段的宽度会自适应    
NSArray *titleArr = @[@"天",@"地",@"人"];       //用数组来初始化   
 UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:titleArr];      
 //整条的frame   
 seg.frame = CGRectMake(0, 64, 320, 40);    [self.view addSubview:seg];  
  [seg release];       
//1.设置初始位置   
 seg.selectedSegmentIndex = 0;     
 //2.向segment中插入一个新的按钮   
 [seg insertSegmentWithTitle:@"Dog" atIndex:2 animated:YES];    
//3.添方法  
 [seg addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];}

//eg1.seg触发方法
-(void)segmentValueChanged:(UISegmentedControl*)seg{   
 NSString *title = [seg titleForSegmentAtIndex:seg.selectedSegmentIndex];    NSLog(@"%@",title);}

你可能感兴趣的:(UISegmentControl的使用)