内涵段子上面的那个选择控件就是UISegmentedControl,一般应用场景基本上就是这种,功能类似UIButton。
- (void)viewDidLoad {
[super viewDidLoad];
_se=[[UISegmentedControl alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, 40)];
//添加元素
[_se insertSegmentWithTitle:@"1" atIndex:0 animated:NO];
[_se insertSegmentWithTitle:@"2" atIndex:1 animated:NO];
//设置默认选择。注意一定要写在元素添加下面,不然的话不会生效。
_se.selectedSegmentIndex=0;
//设置选择不同元素执行的方法
[_se addTarget:self action:@selector(change) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_se];
}
-(void)change{
NSLog(@"%ld",_se.selectedSegmentIndex);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}