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 ...
随机推荐
- selenium python (十四)上传文件的处理
#!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip' #上传过程一般要打开一个系统的windows窗口,从窗口选择本地文件添加 ...
- Java之--Java语言基础组成—函数
Java语言基础组成-函数 Java语言由8个模块构成,分别为:关键字.标识符(包名.类名.接口名.常量名.变量名等).注释.常量和变量.运算符.语句.函数.数组. 本片主要介绍Java中的函数,函数 ...
- php生成百度新闻源xml
<?php /* http://baike.baidu.com/view/125547.htm#2 百度网新闻开放协议 */ mysql_connect($CFG['db_host'] ,$CF ...
- 再来说说Activity
经过前面多天的了解,现在可以确信一点: activity提供了用户和程序交互的界面. 而且android里有四大组件:Activity,Service,BroadcastReceiver,Conten ...
- Use weakref module in a cache or mapping
The weakref module allows the Python programmer to create weak references to objects. In the followi ...
- 移动端的日期插件 mobiscroll 2.14.4 破解版
官方报价695美元 http://mobiscroll.com/pricing 这个 mobiscroll 2.14.4 破解版 包括datetime和calendar组件,包括mobiscroll和 ...
- struts2类库下载
struts2开发包下载 到http://struts.apache.org/download.cgi#struts2014下载struts-2.x.x-all.zip,目前最新版为2.1.6.下载完 ...
- Hadoop系列(二)hadoop2.2.0伪分布式安装
一.环境配置 安装虚拟机vmware,并在该虚拟机机中安装CentOS 6.4: 修改hostname(修改配置文件/etc/sysconfig/network中的HOSTNAME=hadoop),修 ...
- 基于AWS的自动化部署实践
过年前,我给InfoQ写了篇文章详细介绍我们团队在过去4年基于AWS的自动化部署实践.文章包括了:为什么选择AWS.AWS上自动化部署的优势和挑战.我们的解决方案,以及和AWS DevOps方案(Op ...
- geeksforgeeks@ Maximum Index (Dynamic Programming)
http://www.practice.geeksforgeeks.org/problem-page.php?pid=129 Maximum Index Given an array A of int ...