zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    c--日期和时间函数
    笔试题:360找镇长的题。
    【JavaScript】BOM和DOM
    也谈在 .NET 平台上使用 Scala 语言(续)
    生成n个元素的全排列 C实现
    jsp安全性问题
    stm32DMA通道 ADC通道
    POJ 1860
    Codeforces Round #FF (Div. 2) A. DZY Loves Hash
    Configure the modules to be find by modprobe
  • 原文地址:https://www.cnblogs.com/yyt-hehe-yyt/p/4871994.html
Copyright © 2011-2022 走看看