• iOS开发技巧


    (Swift)

    import UIKit
    class ViewController: UIViewController {
        var segmentedControl:UISegmentedControl!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            let segments = [
                "iPhone",
                "iPad",
                "iPod",
                "iMac"]
            
            segmentedControl = UISegmentedControl(items: segments)
            segmentedControl.center = view.center
            
            segmentedControl.addTarget(self,
                action: "segmentedControlValueChanged:",
                forControlEvents: .ValueChanged)
            
            self.view.addSubview(segmentedControl)
        }
        
        func segmentedControlValueChanged(sender: UISegmentedControl){
            let selectedSegmentIndex = sender.selectedSegmentIndex
            let selectedSegmentText = sender.titleForSegmentAtIndex(selectedSegmentIndex)
            println("Segment (selectedSegmentIndex) with text" + " of (selectedSegmentText) is selected")
        }
    }

    (Objective-C)

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @property (nonatomic, strong) UISegmentedControl *mySegmentedControl;
    
    @end
    
    @implementation ViewController
    ...
    - (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];
        
        [self.mySegmentedControl addTarget:self
            action:@selector(segmentChanged:)
            forControlEvents:UIControlEventValueChanged];
    }
    
    - (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);
        }
    }

    (Swift)

    let segments = NSArray(objects:
        "Red",
        UIImage(named: "blueDot")!,
        "Green",
        "Yellow")
    segmentedControl = UISegmentedControl(items: segments)

    (Objective-C)

    NSArray *segments = @[
        @"iPhone",
        [UIImage imageNamed:@"iPad"],
        @"iPod",
        @"iMac",
        ];
        
    self.mySegmentedControl = [[UISegmentedControl alloc]
        initWithItems:segments];
        
    CGRect segmentedFrame = self.mySegmentedControl.frame;
    segmentedFrame.size.height = 128.0f;
    segmentedFrame.size.width = 300.0f;
    self.mySegmentedControl.frame = segmentedFrame;
    
    self.mySegmentedControl.center = self.view.center;
    
    [self.view addSubview:self.mySegmentedControl];
  • 相关阅读:
    CentOS 7 修改时区
    flink与kafka结合
    Kafka 原理和实战
    kafka单机多节点部署
    使用yumdownloadonly下载RPM包及依赖包
    使用Onvif协议进行设备发现以及指定设备信息探测
    kafka相关脚本操作
    Python入门篇-基础语法
    Python入门篇-pyenv安装及应用
    zabbix Server 4.0监控Flume关键参数
  • 原文地址:https://www.cnblogs.com/davidgu/p/5773854.html
Copyright © 2020-2023  润新知