单词:segmented被分割的 index指针、指标current现在的
代码创建
NSArray *itemName = [NSArray arrayWithObjects:@"鸡翅",@"排骨", nil];
UISegmentedControl *segSC = [[UISegmentedControl alloc] initWithItems:itemName];
// segSC.segmentedControlStyle =UISegmentedControlSegmentCenter;
[segSC addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segSC.frame=CGRectMake(0, 200, 320, 100);
[self.view addSubview:segSC];
添加点击事件
-(void)segmentAction:(id)sender
{
switch ([sender selectedSegmentIndex]) {
case 0:
{
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了鸡翅" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alter show];
}
break;
case 1:
{
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了排骨" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alter show];
}
break;
default:
break;
}
}
拖拽创建
1创建并设置属性
拖拽UISegmentedControl到Main.storyboard得页面中,选中它在控制面板中设置它的属性
2.关联事件
选中UISegmentedControl 按住control键拖拽到@implementation ViewController下
3.添加事件
在括号内添加点击效果
- (IBAction)scValueChange:(UISegmentedControl *)sender {
{
int currentIndex = sender.selectedSegmentIndex;//当前选中的分割指针
NSString *title = [sendertitleForSegmentAtIndex:currentIndex];//选中行的标题
NSLog(@"%@",title);
}