UI控件(UISegmentedControl)
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSArray* segmentArray = [[NSArray alloc]initWithObjects:@"Title1", @"Title2", @"Title3",nil]; //也可以通过insertSegmentWithtitile方法添加选项 //也可以对某个选项选择添加图片 UISegmentedControl* segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentArray]; segmentedControl.frame = CGRectMake(30, 20, 250, 30); //设置选项卡的默认点击项,从索引0开始 segmentedControl.selectedSegmentIndex = 0; //分段选项卡的颜色,默认为蓝色 segmentedControl.tintColor = [UIColor redColor]; //设置选项卡在点击后是否会恢复原状 segmentedControl.momentary = NO; //segmentedControlStyle 在IOS 7以后无效 //segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled; //添加选项卡选择事件 [segmentedControl addTarget:self action:@selector(doSelected:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:segmentedControl]; } -(void)doSelected:(id)sender{ UISegmentedControl* tmp = (UISegmentedControl*)sender; NSLog(@"%ld", tmp.selectedSegmentIndex); } @end