标签:ui 控件 uisegmente
- (void)createSegmentControl
{
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"全部", @"分类", @"进行中"]];
segmentedControl.frame = CGRectMake(80, 30, 180, 30);
// 默认选中第一个
segmentedControl.selectedSegmentIndex = 0;
// 改变控件和字体颜色
[segmentedControl setTintColor:[UIColor redColor]];
// 改变某一个 segmentedControl 的标题
[segmentedControl setTitle:@"不是全部" forSegmentAtIndex:0];
// 设置图片
[segmentedControl setImage:[UIImage imageNamed:@"2.png" ] forSegmentAtIndex:2];
// 获取共有多少项
NSLog(@"按钮共有%lu个", segmentedControl.numberOfSegments);
[self addSubview:segmentedControl];
[segmentedControl release];
// 给 segmentedControl 绑定点击事件(最重要)
[segmentedControl addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];
}
- (void)segmentedControlAction:(UISegmentedControl *)sender
{
// 通过 selectedSegmentIndex 判断点中了哪一个 Segment
/*
if (sender.selectedSegmentIndex == 0) {
NSLog(@"全部");
} else if (sender.selectedSegmentIndex == 1) {
NSLog(@"分类");
} else if (sender.selectedSegmentIndex == 2) {
NSLog(@"进行中");
}
*/
switch (sender.selectedSegmentIndex) {
case 0:
{
NSLog(@"全部");
break;
}
case 1:
NSLog(@"分类");
break;
case 2:
NSLog(@"进行中");
break;
default:
break;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:ui 控件 uisegmente
原文地址:http://blog.csdn.net/yadong_zhao/article/details/46777783