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. js画线

    <body> <div id="main"> </div> <div id="fd" style="filt ...

  2. delphi 中 $是什么意思 串口中使用

    delphi 中 $是什么意思? 比如:$41----$5A 意识是26个字母, 可以用$来表示? $在delphi 中还可以怎么用?1.表示16进制,$41就是65,第一个字母的ASCII值 pro ...

  3. Zabbix监控Linux磁盘I/O

    东西都上传到这里了: https://github.com/RexKang/Zabbix/tree/master/OS/Linux-disk-discovery   需要用到的东西: Zabbix的L ...

  4. leetcode:Integer to Roman(整数转化为罗马数字)

    Question: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the rang ...

  5. C语言实现strcat

    首先看看代码: #ifndef STRCAT_H #define STRCAT_H /********************************************************* ...

  6. flappy pig小游戏源码分析(1)——主程序初探

    闲逛github发现一个javascript原生实现的小游戏,源码写的很清晰,适合想提高水平的同学观摩学习.读通源码后,我决定写一系列的博客来分析源码,从整体架构到具体实现细节来帮助一些想提高水平的朋 ...

  7. Container View Controller

    有时候,我们的Controler中包含有另一个controler view的view时,可以使用这种方式. https://developer.apple.com/library/ios/featur ...

  8. Android调用系统自带的文件管理器进行文件选择并读取

    先调用: intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); //设置类型,我这里是任意类 ...

  9. ubuntu 14.04 root破解

    Advanced Programmable interrupt controller 高级可编程中断控制;Advanced create table `users` (`id` int(11) not ...

  10. 【多线程】Java并发编程:并发容器之CopyOnWriteArrayList(转载)

    原文链接: http://ifeve.com/java-copy-on-write/ Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容 ...