分段控件,提供了一组按钮,但是只能激活一个。通过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);

}

UISegmentedControl——分段控件的更多相关文章

  1. ios 初体验< UISegmentedControl 分段控件>

     小知识:  数组快速创建 @[@"",@"",@"",@"".......],字典快速创建方法:@{@"&q ...

  2. 【UISegmentedControl】-  分段控件

    一.初始化 二.常见的属性 1.segmentedControlStyle属性:设置基本的样式 2.momentary属性:设置在点击后是否恢复原样 . 3.numberOfSegments属性:只读 ...

  3. iOS:分段控件UISegmentedControl的详细使用

    分段控件:UISegmentedControl   功能:分段的控制.页面的切换等.   介绍:当用户输入不仅仅是布尔值时,可使用分段控件(UISegmentedControl).分段控件提供一栏按钮 ...

  4. 使用开关、分段控件和web视图

    #import "XViewController.h" @interface XViewController () @end @implementation XViewContro ...

  5. iOS系列 基础篇 09 开关、滑块和分段控件

    iOS系列 基础篇 09 开关.滑块和分段控件 目录: 案例说明 开关控件Switch 滑块控件Slider 分段控件Segmented Control 1. 案例说明 开关控件(Switch).滑块 ...

  6. [Swift通天遁地]九、拔剑吧-(4)使用开源类库创建可滑动的Segment分段控件

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. UISwitch(开关控件)、UISegmentedControl(分段控件)

    一.UISwitch 1.初始化 UISwitch *s1 = [[UISwitch alloc]initWithFrame:CGRectMake(50, 170, 100, 200)];   2.设 ...

  8. iOS在导航栏上居中显示分段控件(UISegmentedControl)

    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:nil]; segmentedCont ...

  9. UISegmentedControl 控件

    一.创建 UISegmentedControl* mySegmentedControl = [[UISegmentedControl alloc]initWithItems:nil]; 是不是很奇怪没 ...

随机推荐

  1. HttpSession详解

    HttpSession详解   session的机制 http是无状态的协议,客户每次读取web页面时,服务器都打开新的会话,而且服务器也不会自动维护客户的上下文信息,那么要怎么才能实现会话跟踪呢?s ...

  2. ntp-keygen.c

    这个程序产生加密数据文件使用的的密码,遵循Autokey security protocol和NTPv4.文件名被名字和创建时间组成的头部当做前缀,后面跟有一个类型定义的描述符标签和PEM加密的数据结 ...

  3. 解决 UNMOUNTABLE_BOOT_VOLUME 蓝屏【转载】

    现象:一台XP系统的机器,开机在滚动条阶段蓝屏,蓝屏代码大概是“UNMOUNTABLE_BOOT_VOLUME”. 用PE进系统后发现C盘的文件格式变为RAW,总容量等变为0 解决方法一:将故障机的硬 ...

  4. 分布式数据库Cobar

    Cobar简介: Cobar是关系型数据库的分布式处理系统,它可以在分布式的环境下看上去像传统数据库一样为您提供海量数据服务. 产品在阿里巴巴B2B公司已经稳定运行了3年以上. 目前已经接管了3000 ...

  5. andorid 自定义view属性declare-styleable

    1. reference:参考某一资源ID. (1)属性定义: <declare-styleable name = "名称"> <attr name = &quo ...

  6. jna

    http://topmanopensource.iteye.com/blog/1752355

  7. win7 64位andriod开发环境搭建

    本文转自:http://www.cfanz.cn/index.php?c=article&a=read&id=65289 最近换了新电脑,装了win7 64位系统,安装了各种开发环境, ...

  8. (转)Java 之 FileReader FileInputStream InputStreamReader BufferedReader 作用与区别

    Java 之 FileReader FileInputStream InputStreamReader BufferedReader 作用与区别 ava.io下面有两个抽象类:InputStream和 ...

  9. Android---->FrameLayout布局方式

    main.xml布局代码分析-->实现一个渐变图案 <?xml version="1.0" encoding="utf-8"?> <Fr ...

  10. brbustoj 1818 石子合并问题--直线版

    比较经典且基础的区间dp,转移方程为 dp_max[i][j] = max(dp_max[i][j],dp_max[i][k] + dp_max[k+1][j] + sum[j]-sum[i-1]); ...