IOS 7 Study - UISegmentedControl

简介: You would like to present a few options to your users from which they can pick anoption, through a UI that is compact, simple, and easy to understand.

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开发 索引
iOS UISegmentedControl
iOS UISegmentedControl
478 0
|
iOS开发 Swift
iOS开发技巧 - 使用UISegmentedControl来对信息进行分组
    (Swift) import UIKit class ViewController: UIViewController { var segmentedControl:UISegmentedControl! override func viewDidLoad() { super.
1107 0
|
iOS开发 Swift
iOS - UISegmentedControl
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UISegmentedControl : UIControl @available(iOS 2.0, *) public class UISegmentedControl : UIControl, NSCoding UISegmentedControl 的 Items 有两种样式,一种是字符串设置的标题样式,一种是图片设置的样式。
1397 0
|
iOS开发
IOS UISegmentedControl详解
http://www.cnblogs.com/top5/archive/2012/05/17/2506618.html
836 0
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台策略
在移动应用开发的战场上,安卓和iOS两大阵营各据一方。随着技术的演进,跨平台开发框架成为开发者的新宠,旨在实现一次编码、多平台部署的梦想。本文将探讨跨平台开发的优势与挑战,并分享实用的开发技巧,帮助开发者在安卓和iOS的世界中游刃有余。
|
iOS开发 开发者
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
1232 67
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
|
iOS开发 开发者 MacOS
深入探索iOS开发中的SwiftUI框架
【10月更文挑战第21天】 本文将带领读者深入了解Apple最新推出的SwiftUI框架,这一革命性的用户界面构建工具为iOS开发者提供了一种声明式、高效且直观的方式来创建复杂的用户界面。通过分析SwiftUI的核心概念、主要特性以及在实际项目中的应用示例,我们将展示如何利用SwiftUI简化UI代码,提高开发效率,并保持应用程序的高性能和响应性。无论你是iOS开发的新手还是有经验的开发者,本文都将为你提供宝贵的见解和实用的指导。
675 66

热门文章

最新文章