• 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];
  • 相关阅读:
    win10删除了应用商店重新安装,应用商店报错不能联网下载
    bps,Bps,pps,fps单位换算
    mongodb查不出数据
    ps切图
    ppt常用设置
    视频录制
    excel设置下拉
    小程序开发
    手把手从0搭建hexo博客
    两种方式实现登录跳转新窗体
  • 原文地址:https://www.cnblogs.com/davidgu/p/5773854.html
Copyright © 2020-2023  润新知