用处:多分段选择控件,选中某个分段会触发值改变事件。
-(void)viewDidLoad
{
[super viewDidLoad];
NSArray* items = @[@"a",@"b",@"c"];
self.seg = [[UISegmentedControl alloc]initWithItems:items];
CGRect frame = CGRectMake(20,20,180,50);
self.seg.frame = frame;
//设置索引的标题
[self.seg setTitle:@"two" forSegmentAtIndex:1];
//设置默认选择器索引
self.seg.selectedSegmentIndex = 1;
//设置底色
self.seg.tintColor = [UIColor redColor];
//设置样式
self.seg.segmentedControlStyle = UISegmentedControlStyleBar;
//设置指定索引选项不可选
[self.seg setEnabled:NO forSegmentAtIndex:2];
//添加值改变事件
[self.seg addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
[self.view addSubView:self.seg];
}
-(IBAction) change:(id)sender
{
UISegmentControl *seg = (UISegmentControl*) sender;
int index = seg.selectedSegmentIndex;
NSString* title = [self.seg titleForSegmentAtIndex:index];
NSLog(@"%@",title);
}