1.UISegmentControl
1)先创建一个数组用来存放标题
NSArray *array = @[@"1",@"2",@"3",nil];
UISegmentedControl *segmented = [[UISegmentedControl alloc] initWithItems:array];
[segmented insertSegmentWithTitle:@"3" atIndex:3 animated:NO];
[segmented removeSegmentAtIndex:3 animated:NO];
[segmented setImage:[UIImage ImageNamed:@"2.png"] forSegmentAtIndesx:1];
[segmented setTitle:@"分组1" forSegmentAtIndex:0];
[segmented setWidth:60 fotSegmentAtIndex:0];
//UISegmented的点击事件,后面的控制事件的状态一定是valueChanged
[segmented addTarget:self action:@selector(segmentAction:) forControlEvent:UIControlEventValueChanged];
2.通常UISegmentedControl的点击事件和switch case结合使用
- (void)segmentedAction:(UISegmentedControl *)segmented {
switch (segmented.selectedSegmentIndex) {
case 0 :
self.backGroundColor = [UIColor yellowColor];
break;
default:
break;
}
}