IOS 7 Study - UISegmentedControl
You would like to present a few options to your users from which they can pick an
option, through a UI that is compact, simple, and easy to understand.
effect:

1. declare control
#import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UISegmentedControl *mySegmentedControl; @end @implementation ViewController
2. create the segmented control in the viewDidLoad method of your view controller
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *segments = [[NSArray alloc] initWithObjects:
@"iPhone",
@"iPad",
@"iPod",
@"iMac", nil];
self.mySegmentedControl = [[UISegmentedControl alloc]
initWithItems:segments];
self.mySegmentedControl.center = self.view.center;
[self.view addSubview:self.mySegmentedControl];
}
3. use the addTarget:action:forControlEvents: method of the segmented control to
recognize when the user selects a new option
// add event listener
[self.mySegmentedControl addTarget:self
action:@selector(segmentChanged:)
forControlEvents:UIControlEventValueChanged];
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *segments = @[
@"iPhone",
@"iPad",
@"iPod",
@"iMac"
];
self.mySegmentedControl = [[UISegmentedControl alloc]
initWithItems:segments];
self.mySegmentedControl.center = self.view.center;
[self.view addSubview:self.mySegmentedControl];
[self.mySegmentedControl addTarget:self
action:@selector(segmentChanged:)
forControlEvents:UIControlEventValueChanged];
}
4. segment change event
- (void) segmentChanged:(UISegmentedControl *)paramSender {
if ([paramSender isEqual:self.mySegmentedControl]) {
NSInteger selectedSegmentIndex = [paramSender selectedSegmentIndex];
NSString *selectedSegmentText =
[paramSender titleForSegmentAtIndex:selectedSegmentIndex];
NSLog(@"Segment %ld with %@ text is selected",
(long)selectedSegmentIndex,
selectedSegmentText);
}
}
result on console:
Segment 0 with iPhone text is selected
Segment 1 with iPad text is selected
Segment 2 with iPod text is selected
Segment 3 with iMac text is selected
If no item is selected, this method returns the value –1
IOS 7 Study - UISegmentedControl的更多相关文章
- IOS UI segmentedControl UISegmentedControl 常见属性和用法
UISegmentedControl中一些常见的属性和用法 //设置以图案作为分段的显示,仅需要图案的轮廓,这样颜色为分段的背景颜色 // NSArray *items = @[[UIImage ...
- IOS 7 Study - UIViewController
Presenting and Managing Views with UIViewController ProblemYou want to switch among different views ...
- ios 初体验< UISegmentedControl 分段控件>
小知识: 数组快速创建 @[@"",@"",@"",@"".......],字典快速创建方法:@{@"&q ...
- IOS 7 Study - Displaying an Image on a Navigation Bar
ProblemYou want to display an image instead of text as the title of the current view controlleron th ...
- IOS 7 Study - Manipulating a Navigation Controller’s Array of View
ProblemYou would like to directly manipulate the array of view controllers associated with aspecific ...
- IOS 7 Study - Implementing Navigation with UINavigationController
ProblemYou would like to allow your users to move from one view controller to the other witha smooth ...
- IOS 7 Study - UIActivityViewController(Presenting Sharing Options)
You want to be able to allow your users to share content inside your apps with theirfriends, through ...
- IOS 7 Study - UIDatePicker
Picking the Date and Time with UIDatePicker effect: 1. declaring a property of type UIDatePicker #im ...
- ios 如何改变UISegmentedControl文本的字体大小?
UIFont *Boldfont = [UIFont boldSystemFontOfSize:16.0f]; NSDictionary *attributes = [NSDictionary dic ...
随机推荐
- 模式自由(Schema-free)和数据存储的非格式化趋势
最近遐想,数据存储的非格式化趋势. 格式化表格到自由的XML存储 数年以前,多家数据库厂商开始XML数据库存储.XML数据作为一种自描述的半结构化数据为Web的数据管理提供了新的数据模型,如果将XML ...
- Net判断一个对象是否为数值类型 z
http://www.cnblogs.com/SkyD/p/4053461.html public static bool IsNumeric(this Type dataType) { if (da ...
- Delphi Val函数
在这里Val和iif都是你所用的数据库中的函数在delphi中Val是一个将字符串转换为数字的函数,Val(S; var V; var Code: Integer)第一个参数是要转换的字符串,第二个参 ...
- 如何在Docker中部署DzzOffice
一.一些背景 之前研究Docker很久了,并且在公司内部实际使用起来了,目前分两种场景使用Docker 1.作为PAAS,提供一致,统一的编译/测试环境: 2.作为虚拟机,直接分配给新来的开发人员使用 ...
- C++ 中类的构造函数理解(二)
C++ 中类的构造函数理解(二) 写在前面 上次的笔记中简要的探索了一下C++中类的构造函数的一些特性,这篇笔记将做进一步的探索.主要是复制构造函数的使用. 复制构造函数 复制构造函数也称拷贝构造函数 ...
- JDK - Tomcat - JSP - Servlet 配置运行全攻略(转)
http://www.cnblogs.com/myqiao/archive/2005/08/29/225497.html 花了将近两个月的时间,从 JDK 开始一步一步摸索,历经千辛万苦,终于让第一个 ...
- scrollTop
scrollTop 表示滚动的高度,默认从position:0;开始向下滚,scrollTop(offset)的offset表示相对顶部的偏移,以像素计,<br/> scrollTop() ...
- Hive QL
转自http://www.alidata.org/archives/581 Hive 的官方文档中对查询语言有了很详细的描述,请参考:http://wiki.apache.org/hadoop/Hiv ...
- 上传控件swfupload的使用笔记
1.下载下来的官方domo里不同的例子里会引入各自的JS,注意区分.可以直接拿官方例子来改成自己想要的例子. 2.注意PHP配置文件里也有最大上传文件限制,如果文件太大会上传不成功. 3.如果有问题可 ...
- [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序
一. 题目 487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 274040 Accepted: 48891 ...