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的更多相关文章

  1. IOS UI segmentedControl UISegmentedControl 常见属性和用法

    UISegmentedControl中一些常见的属性和用法 //设置以图案作为分段的显示,仅需要图案的轮廓,这样颜色为分段的背景颜色 //    NSArray *items = @[[UIImage ...

  2. IOS 7 Study - UIViewController

    Presenting and Managing Views with UIViewController ProblemYou want to switch among different views ...

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

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

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. IOS 7 Study - UIDatePicker

    Picking the Date and Time with UIDatePicker effect: 1. declaring a property of type UIDatePicker #im ...

  9. ios 如何改变UISegmentedControl文本的字体大小?

    UIFont *Boldfont = [UIFont boldSystemFontOfSize:16.0f]; NSDictionary *attributes = [NSDictionary dic ...

随机推荐

  1. 自定义控件如何给特殊类型的属性添加默认值 z

    定义控件如何给特殊类型的属性添加默认值了,附自定义GroupBox一枚 标题有点那啥,但确实能表达我掌握此法后的心情. 写自定义控件时往往会有一个需求,就是给属性指定一个默认值(就是可以在VS中右键该 ...

  2. 生产环境服务CPU高问题分析

    问题描述: 现网个别时候会出现CPU突然飙高的现象,飙高后不能恢复正常. 分析过程: CPU飙高后抓dump,最好本机看,其它机器看dump可能需要下载服务运行机器的sos,clr     0:000 ...

  3. STM32 UART 重映射

    在进行原理图设计的时候发现管脚的分配之间有冲突,需要对管脚进行重映射,在手册中了解到STM32 上有很多I/O口,也有很多的内置外设像:I2C,ADC,ISP,USART等 ,为了节省引出管脚,这些内 ...

  4. VS如何设置OpenCV静态编译

      可以使用opencv提供的静态链接库也可以自己编译静态链接库. 1 使用opencv提供的静态链接库,位置如下图. 首先设置VS配置.有如下几个配置 1 工具->选项->项目和解决方案 ...

  5. 我来说说MVC过滤器

    APS.NET MVC中的每一个请求,都会分配给相应的控制器和对应的行为方法去处理,而在这些处理的前前后后如果想再加一些额外的逻辑处理.这时候就用到了过滤器. 在Asp.netMvc中当你有以下及类似 ...

  6. Window Redis分布式部署方案 java

    Redis分布式部署方案 Window 1.    基本介绍 首先redis官方是没有提供window下的版本, 是window配合发布的.因现阶段项目需求,所以研究部署的是window版本的,其实都 ...

  7. JavaScript之Object

    两种简单的JavaScript中定义对象的方式: 在JavaScript中,可以动态添加对象的属性,也可以动态删除对象的属性. var object=new object(); //alert(obj ...

  8. mysql innobackupex xtrabackup 大数据量 备份 还原(转)

    原文:http://blog.51yip.com/mysql/1650.html 作者:海底苍鹰 大数据量备份与还原,始终是个难点.当MYSQL超10G,用mysqldump来导出就比较慢了.在这里推 ...

  9. TransactionScope事务对多个数据库的操作

    .Net 2.0引入了轻量级事务管理器(Lighweight Transaction Manager),即System.Transactions.TransactionManager. 轻量级事务管理 ...

  10. KMP算法——Javascript实现

    腾讯和阿里的笔试刚过去了,里面有很多题都很值得玩味的.之前Blog积累的很多东西,还要平时看的书,都有很大的帮助.这个深有体会啊! 例如,腾讯有一道算法题是吃香蕉(好邪恶的赶脚..),一次吃一根或者两 ...