root。m
#import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; //创建一个分段控件 NSArray *array =[NSArray arrayWithObjects:@"亚洲",@"欧美",@"日韩", nil]; UISegmentedControl *seg =[[UISegmentedControl alloc]initWithItems:array]; seg.frame =CGRectMake(30, 100, 300, 50); [self.view addSubview:seg]; //修改颜色 // seg.backgroundColor=[UIColor yellowColor]; seg.tintColor=[UIColor redColor]; //默认选择某一个 seg.selectedSegmentIndex =1; //修改标题 [seg setTitle:@"动漫" forSegmentAtIndex:2]; // 添加图片 png可省略后缀 jpg不可省略 设置选项卡图标 UIImage *image =[UIImage imageNamed:@"a.jpg"]; //防止图片被控件渲染(保持自己的颜色) image =[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; [seg setImage:image forSegmentAtIndex:0]; //添加事件 [seg addTarget:self action:@selector(selectWhich:) forControlEvents:UIControlEventValueChanged]; } -(void)selectWhich:(UISegmentedControl *)se { NSLog(@"%d",se.selectedSegmentIndex); if(se.selectedSegmentIndex==2){ NSLog(@"正在打开动漫"); } } @end