• UIPickerView


      简单的介绍一下UIPickerView,下面以单个选择器举例。

    #import "ViewController.h"

    //设置UIPickerView的协议

    @interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

    @property(nonatomic,strong) UIPickerView *pickerView;

    @property(nonatomic,strong) NSArray *testArr;

    @property(nonatomic,strong) UILabel *textLabel;

    @end

    @implementation ViewController

    -(NSArray *)testArr {

        if (!_testArr) {

            _testArr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25"];

        }

        return _testArr;

    }

    - (void)viewDidLoad {

        [super viewDidLoad];

        //显示指定项的内容

        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((self.view.bounds.size.width - 200)/2, 100, 200, 40)];

        self.textLabel = label;

        [self.view addSubview:label];

        //弹出选择器

        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

        button.frame = CGRectMake((self.view.bounds.size.width - 100)/2, 200, 100, 40);

        button.backgroundColor = [UIColor redColor];

        [button setTitle:@"hehe" forState:UIControlStateNormal];

        [button addTarget:self action:@selector(clickingButton) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:button];

    }

    - (void)clickingButton {

        UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(20, self.view.bounds.size.height - 250, self.view.bounds.size.width - 40, 250)];

        self.pickerView = pickerView;

        pickerView.showsSelectionIndicator = YES;

        pickerView.dataSource = self;

        pickerView.delegate = self;

        [self.view addSubview:pickerView];

    }

     //设定选择器的个数

    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

        return 1;

    }

     //指定每个选择器中选项数

    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

        return self.testArr.count;

    }

     //指定每个选项的内容

    -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

        NSString *str = self.testArr[row];

        return str;

    }

     //选定指定项之后的操作

    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

        self.textLabel.text = self.testArr[row];

        [self.pickerView removeFromSuperview];

    }

    @end

  • 相关阅读:
    转载算法达人修炼营实践模板
    win7上面已经安装了mysql,但是net start mysql提示服务名无效
    如何读懂c++源码?
    struts2 DMI无法运行
    spring下配置dbcp,c3p0,proxool
    前端VS后台
    jsp以及servlet中文乱码问题
    文件上传
    注册和验证的实现
    网站访问计数器的设计
  • 原文地址:https://www.cnblogs.com/yyt-hehe-yyt/p/4871994.html
Copyright © 2020-2023  润新知