博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS 7 Study - UISegmentedControl
阅读量:6572 次
发布时间:2019-06-24

本文共 2275 字,大约阅读时间需要 7 分钟。

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

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/davidgu/p/3543411.html

你可能感兴趣的文章
Mysql 数据库学习笔记02 编程
查看>>
堆排序
查看>>
解决PyScripter中文乱码问题
查看>>
Python: 分数运算
查看>>
信息隐藏技术与应用期末复习
查看>>
ulimit命令学习
查看>>
js - 预加载+监听图片资源加载制作进度条
查看>>
[BZOJ1934][Shoi2007]Vote 善意的投票[最小割]
查看>>
sort()排序
查看>>
Windows IO 性能简单测试
查看>>
HDU-1796 How many integers can you find 容斥定理
查看>>
css display&&hidden
查看>>
不使用border-radius,实现一个可复用的高度和宽度都自适应的圆角矩形
查看>>
平衡二叉树——Balance Binary Sort Tree 设计与实现
查看>>
https
查看>>
js动态加载css文件和js文件的方法
查看>>
HTML中的table和div
查看>>
SqlServer整库备份还原脚本
查看>>
使用Github发布自己的网站
查看>>
2019-04-28 Mybatis generator逆向工程生成的Example代码分析
查看>>