UISegmentedControl
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UISegmentedControl相关的知识,希望对你有一定的参考价值。
分段控件提供了?栏按钮,但是每次只能激活?个按钮,每?个按钮对应不同的屏幕显?的东西(这?的不同,应该理解为数据的不同,view是相同的,如筛选出不同的信息,但是view是?样的(布局样式是?样的))。
//
// ViewController.m
// UISegmentedControl01
//
// Created by cqy on 16/2/15.
// Copyright © 2016年 程清杨. All rights reserved.
//
#import "ViewController.h"
@interface ViewController (){
UISegmentedControl *seg;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建segmentcontrol
NSMutableArray *itemArr = [NSMutableArray array];
[itemArr addObject:@"first"];
[itemArr addObject:@"second"];
[itemArr addObject:@"thied"];
//[[UISegmentedControl alloc] initWithItems:itemArr];为初始化?法,是UISegmentedControl特有的初始化?法。initWithItems:的参数是?个数组。
seg = [[UISegmentedControl alloc] initWithItems:itemArr];
//设置frame
seg.frame = CGRectMake(50, 50, 200, 50);
//设置分栏颜色
seg.tintColor = [UIColor blueColor];
//默认选中
seg.selectedSegmentIndex = 0;
//添加点击事件
[seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:seg];
//在这?添加?个button,每点击?次button,就会改变?次segment的标题:setTitle:@"第?项" forSegmentAtIndex:0说明把第0个分段的标题设置成“第?项”。
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(50, 150, 50, 50);
btn.backgroundColor = [UIColor blueColor];
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)btnAction:(UIButton *)sender{
NSLog(@".....");
//设置分段上的文字
[seg setTitle:@"第一项" forSegmentAtIndex:0];
[seg setTitle:@"第二项" forSegmentAtIndex:1];
[seg setTitle:@"第三项" forSegmentAtIndex:2];
[seg insertSegmentWithTitle:@"第四项" atIndex:3 animated:YES];
}
-(void)segAction:(UISegmentedControl *)sender{
//添加segment点击事件:第?个参数:谁来执?,第?个参数,到谁那?去找segAction?法,然后执?,第三个参数:事件改变的时候才执?。
NSLog(@"%ld",sender.selectedSegmentIndex);
if (sender.selectedSegmentIndex == 0) {
[sender setTintColor:[UIColor redColor]];
}else if (sender.selectedSegmentIndex == 1){
[sender setTintColor:[UIColor greenColor]];
}else if (sender.selectedSegmentIndex == 2){
[sender setTintColor:[UIColor brownColor]];
}else{
[sender setTintColor:[UIColor yellowColor]];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// ViewController.m
// UISegmentedControl01
//
// Created by cqy on 16/2/15.
// Copyright © 2016年 程清杨. All rights reserved.
//
#import "ViewController.h"
@interface ViewController (){
UISegmentedControl *seg;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建segmentcontrol
NSMutableArray *itemArr = [NSMutableArray array];
[itemArr addObject:@"first"];
[itemArr addObject:@"second"];
[itemArr addObject:@"thied"];
//[[UISegmentedControl alloc] initWithItems:itemArr];为初始化?法,是UISegmentedControl特有的初始化?法。initWithItems:的参数是?个数组。
seg = [[UISegmentedControl alloc] initWithItems:itemArr];
//设置frame
seg.frame = CGRectMake(50, 50, 200, 50);
//设置分栏颜色
seg.tintColor = [UIColor blueColor];
//默认选中
seg.selectedSegmentIndex = 0;
//添加点击事件
[seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:seg];
//在这?添加?个button,每点击?次button,就会改变?次segment的标题:setTitle:@"第?项" forSegmentAtIndex:0说明把第0个分段的标题设置成“第?项”。
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(50, 150, 50, 50);
btn.backgroundColor = [UIColor blueColor];
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)btnAction:(UIButton *)sender{
NSLog(@".....");
//设置分段上的文字
[seg setTitle:@"第一项" forSegmentAtIndex:0];
[seg setTitle:@"第二项" forSegmentAtIndex:1];
[seg setTitle:@"第三项" forSegmentAtIndex:2];
[seg insertSegmentWithTitle:@"第四项" atIndex:3 animated:YES];
}
-(void)segAction:(UISegmentedControl *)sender{
//添加segment点击事件:第?个参数:谁来执?,第?个参数,到谁那?去找segAction?法,然后执?,第三个参数:事件改变的时候才执?。
NSLog(@"%ld",sender.selectedSegmentIndex);
if (sender.selectedSegmentIndex == 0) {
[sender setTintColor:[UIColor redColor]];
}else if (sender.selectedSegmentIndex == 1){
[sender setTintColor:[UIColor greenColor]];
}else if (sender.selectedSegmentIndex == 2){
[sender setTintColor:[UIColor brownColor]];
}else{
[sender setTintColor:[UIColor yellowColor]];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
以上是关于UISegmentedControl的主要内容,如果未能解决你的问题,请参考以下文章
tvos UISegmentedControl 焦点样式不变
UISegmentedControl 值更改时如何仅更改文本颜色?