• UISegmentedControl


    分段控件提供了⼀栏按钮,但是每次只能激活⼀个按钮,每⼀个按钮对应不同的屏幕显⽰的东西(这⾥的不同,应该理解为数据的不同,view是相同的,如筛选出不同的信息,但是view是⼀样的(布局样式是⼀样的))。      
     
    //
    //  ViewController.m
    //  UISegmentedControl01
    //
    //  Created by cqy on 16/2/15.
    //  Copyright © 2016年 程清杨. All rights reserved.
    //

    #import "ViewController.h"

    @interface ViewController (){
        UISegmentedControl *seg;
    }

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        //创建segmentcontrol
        NSMutableArray *itemArr = [NSMutableArray array];
        [itemArr addObject:@"first"];
        [itemArr addObject:@"second"];
        [itemArr addObject:@"thied"];
        //[[UISegmentedControl alloc] initWithItems:itemArr];为初始化⽅法,是UISegmentedControl特有的初始化⽅法。initWithItems:的参数是⼀个数组。
        seg = [[UISegmentedControl alloc] initWithItems:itemArr];
        //设置frame
        seg.frame = CGRectMake(50, 50, 200, 50);
        //设置分栏颜色
        seg.tintColor = [UIColor blueColor];
        //默认选中
        seg.selectedSegmentIndex = 0;
        //添加点击事件
        [seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
       
        [self.view addSubview:seg];
        //在这⾥添加⼀个button,每点击⼀次button,就会改变⼀次segment的标题:setTitle:@"第⼀项" forSegmentAtIndex:0说明把第0个分段的标题设置成“第⼀项”。
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(50, 150, 50, 50);
        btn.backgroundColor = [UIColor blueColor];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
       
        // Do any additional setup after loading the view, typically from a nib.
    }
    - (void)btnAction:(UIButton *)sender{
        NSLog(@".....");
        //设置分段上的文字
        [seg setTitle:@"第一项" forSegmentAtIndex:0];
        [seg setTitle:@"第二项" forSegmentAtIndex:1];
        [seg setTitle:@"第三项" forSegmentAtIndex:2];
        [seg insertSegmentWithTitle:@"第四项" atIndex:3 animated:YES];
    }
    -(void)segAction:(UISegmentedControl *)sender{
        //添加segment点击事件:第⼀个参数:谁来执⾏,第⼆个参数,到谁那⾥去找segAction⽅法,然后执⾏,第三个参数:事件改变的时候才执⾏。
       
        NSLog(@"%ld",sender.selectedSegmentIndex);
       
       
        if (sender.selectedSegmentIndex == 0) {
            [sender setTintColor:[UIColor redColor]];
        }else if (sender.selectedSegmentIndex == 1){
             [sender setTintColor:[UIColor greenColor]];
        }else if (sender.selectedSegmentIndex == 2){
             [sender setTintColor:[UIColor brownColor]];
        }else{
            [sender setTintColor:[UIColor yellowColor]];
        }
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    @end
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    解决vmware Invalid memory setting (sched.mem.min)
    PostgreSQL教程
    rpm包安装过程中依赖问题“libc.so.6 is needed by XXX”解决方法
    使用厂商MIB库查找设备OID值 并实施监控的方法
    【交换机】我司交换机上常用的一些MIB以及对应的OID说明
    CentOS6.8-minimal安装gnome桌面 安装NVC远程桌面连接
    LINUX新建和增加SWAP分区
    Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWo
    类与对象
    Volley框架源代码分析
  • 原文地址:https://www.cnblogs.com/iQingYang/p/5193189.html
Copyright © 2020-2023  润新知