当程序需要用户输入的不仅仅是布尔值,还是多个枚举值时,既可使用分段控件(UISegmentedControl)
提供一栏按钮(有事称为按钮栏)
1.Stryle
Plain:普通
Boredered:普通风格周围添加一圈边框
Bar:如果选择该列表项,该分段控件将会使用工具条风格
2.State
Momentary 复选框 勾选则不会保存空间的状态,当用户点击分段控件的某个分段时,该控件只在用户点击时高亮显示,用户点击结束时,该分段控件不会继续高亮显示用户点击的那个分段
3.Tint
分段控件被选中分段的高亮颜色
4.Segments
整数,控制该分段控件总共被分为几段。
5.Segment
列表框,用于选择指定的分段 计数从零开始
6.Title
为Segment分段设置标题
7.Image
为Segment列表框中选中的分段设置图片
8.Behavior
Enabled:Segment列表框中选中的分段是否可用 取消 不可用
Selected:Segment列表框中选中的分段编程高亮被选中状态
NSMutableDictionary* dict=[self getIrc];
self.segmentedControl=[[UISegmentedControl alloc]initWithFrame:CGRectMake(0, 60, 400, 40)];
[self.segmentedControl insertSegmentWithTitle:@"One" atIndex:0 animated:YES];
[self.segmentedControl insertSegmentWithTitle:@"Two" atIndex:1 animated:YES];
[self.segmentedControl insertSegmentWithTitle:@"Three" atIndex:2 animated:YES];
[self.segmentedControl addTarget:self action:@selector(segmentSelected:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.segmentedControl];
//添加和删除Segment的按钮
UIButton* addSegment=[UIButton buttonWithType:UIButtonTypeRoundedRect];
addSegment.frame=CGRectMake(300, 140, 30, 20);
UIButton* deleteSegment=[UIButton buttonWithType:UIButtonTypeRoundedRect];
deleteSegment.frame=CGRectMake(370, 140, 30, 20);
[addSegment setTitle:@"添加" forState:UIControlStateNormal];
[deleteSegment setTitle:@"删除" forState:UIControlStateNormal];
[addSegment addTarget:self action:@selector(addSegment:) forControlEvents:UIControlEventTouchDown];
[deleteSegment addTarget:self action:@selector(deleteSegment:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:deleteSegment];
[self.view addSubview:addSegment];
//用于输入segmentTitle的文本框
self.textField_segmentText=[[UITextField alloc]initWithFrame:CGRectMake(20, 140, 150, 40)];
self.textField_segmentText.borderStyle=UITextBorderStyleRoundedRect;
self.textField_segmentText.placeholder=@"请输入标题";
[self.view addSubview:self.textField_segmentText];
//
// Do any additional setup after loading the view, typically from a nib.
}
-(void)addSegment:(id)sender{
NSInteger count=self.segmentedControl.numberOfSegments;
NSString* title=self.textField_segmentText.text;
if (count==0) {
count=1;
}
if ([title length]>0) {
[self.segmentedControl insertSegmentWithTitle:title atIndex:count animated:YES];
self.textField_segmentText.text=@"";
}
}
-(void)deleteSegment:(id)sender{
NSInteger count=self.segmentedControl.numberOfSegments;
if (count>0) {
[self.segmentedControl removeSegmentAtIndex:count-1 animated:YES];
}
}
-(void)segmentSelected:(id)sender{
switch (self.segmentedControl.selectedSegmentIndex) {
case 0:
self.view.backgroundColor=[UIColor redColor];
break;
case 1:
self.view.backgroundColor=[UIColor greenColor];
break;
case 2:
self.view.backgroundColor=[UIColor blackColor];
break;
case 3:
self.view.backgroundColor=[UIColor whiteColor];
break;
default:
self.view.backgroundColor=[UIColor whiteColor];
break;
}
}