• IOS 7 Study


    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

     

  • 相关阅读:
    runloop源代码
    runloop的source
    How an Event Enters a Cocoa Application
    RunLoop主要处理以下6类事件
    NSRunloop总结
    performSelector与objc_msgSend
    iOSUI显示思想
    NSPort与NSRunloop的关系是流与消息调度的关系
    Core Animation 负责将bitmap绑定提交到 GPU-[CALayer _display]
    iOS构建流畅的交互界面--CPU,GPU资源消耗的原因和解决方案
  • 原文地址:https://www.cnblogs.com/davidgu/p/3543411.html
Copyright © 2020-2023  润新知