UISegmentedControl——分段控件
2015-08-14 11:09
381 查看
分段控件,提供了一组按钮,但是只能激活一个。通过UIControlEventValueChanged事件实现与用户的交互,并通过selectedSegmentIndex判断当前选定的控件,通过titleForSegmentAtIndex可以获取当前选中控件的标题。
- (void)viewDidLoad {
[super viewDidLoad];
//分段控件中各控件的标题
NSArray *array = @[@"未支付",@"已支付",@"已到货"];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:array];
segmentedControl.frame = CGRectMake(20, 20, self.view.bounds.size.width - 40, 40);
//设置未选中控件的背景色和选择控件的字体颜色
segmentedControl.backgroundColor = [UIColor yellowColor];
//设置选中控件的背景色和未选中控件的字体颜色
segmentedControl.tintColor = [UIColor blueColor];
//设置初始状态下,第一个控件为选中状态
segmentedControl.selectedSegmentIndex = 0;
//设置选择控件触发的事件
[segmentedControl addTarget:self action:@selector(selectSegmentedControl:) forControlEvents:UIControlEventValueChanged];
//设置第二个控件的标题
[segmentedControl setTitle:@"hehe" forSegmentAtIndex:1];
//在第二个控件后插入一个按钮
[segmentedControl insertSegmentWithTitle:@"呵呵" atIndex:2 animated:YES];
[self.view addSubview:segmentedControl];
}
- (void)selectSegmentedControl:(UISegmentedControl*)segmentedControl {
//获取选中状态的标题
NSString *str = [segmentedControl titleForSegmentAtIndex:segmentedControl.selectedSegmentIndex];
NSLog(@"%ld:%@",segmentedControl.selectedSegmentIndex,str);
}
- (void)viewDidLoad {
[super viewDidLoad];
//分段控件中各控件的标题
NSArray *array = @[@"未支付",@"已支付",@"已到货"];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:array];
segmentedControl.frame = CGRectMake(20, 20, self.view.bounds.size.width - 40, 40);
//设置未选中控件的背景色和选择控件的字体颜色
segmentedControl.backgroundColor = [UIColor yellowColor];
//设置选中控件的背景色和未选中控件的字体颜色
segmentedControl.tintColor = [UIColor blueColor];
//设置初始状态下,第一个控件为选中状态
segmentedControl.selectedSegmentIndex = 0;
//设置选择控件触发的事件
[segmentedControl addTarget:self action:@selector(selectSegmentedControl:) forControlEvents:UIControlEventValueChanged];
//设置第二个控件的标题
[segmentedControl setTitle:@"hehe" forSegmentAtIndex:1];
//在第二个控件后插入一个按钮
[segmentedControl insertSegmentWithTitle:@"呵呵" atIndex:2 animated:YES];
[self.view addSubview:segmentedControl];
}
- (void)selectSegmentedControl:(UISegmentedControl*)segmentedControl {
//获取选中状态的标题
NSString *str = [segmentedControl titleForSegmentAtIndex:segmentedControl.selectedSegmentIndex];
NSLog(@"%ld:%@",segmentedControl.selectedSegmentIndex,str);
}
相关文章推荐
- fatal error C1083: 无法打开编译器生成的文件:“../../build/vs71/release/lib_json\json_value.asm”: No such file or directory
- 一些maven引起的Build问题(仅作记录)
- Easyui DataGrid自定义视图和subGrid
- Number Sequence - HDU 1711(KMP模板题)
- easyui-datebox不能选择未来时间的解决方案
- Android UI线程和非UI线程
- ZOJ 1649 Rescue
- 如何监听UIimageview的点击事件以及如何控制UIimageview显示圆形
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- LeetCode:Unique Binary Search Trees(一维DP)
- android android:noHistory="true" 的作用
- Android关于buildToolVersion与CompileSdkVersion的区别
- UITextField使用步骤
- String StringBuffer和StringBuilder的区别
- 在flash builder里全局搜索里如何“全字匹配”搜索
- Havok_2014-1-0_Pc_Xs_User_Guide(2.3.3-碰撞监听器)
- 认识Require
- 如何查看windows系统UUID
- iOS_UITableView 编辑(cell的插入, 删除, 移动)
- android 二维码制作,显示到UI,并保存SD卡,拿来就能用!!