UIPickerView的使用

UIPickerView是很常用的一个UI控件

首先UIPickerView的创建,与多数控件一样,分配内存并设置位置尺寸。

重要的的是代理与数据源,设置代理和数据源后服从代理和数据源协议

<UIPickerViewDelegate,UIPickerViewDataSource>

其中数据源里面有两个必须实现的方法

//设置列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return count;
}

//设置指定列包含的项数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

}

UIPickerView是可以只设置单列的,列被称为component,此时返回的列数为1即可,单更多时候需要的是多列情况,此时设置返回值为自己所需要的列数即可。

在UIPickerViewDataSource数据源协议中,仅仅提供了UIPickerView包含几列以及每一列的项数,而每一行展示的选项是通过UIPickerViewDelegate协议中的方法来设置的。

//设置每个选项显示的内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
}
//获取用户当前选中的选项
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
   
}

UIPickerView基本属性和方法:

设置数据源对象以及代理对象

@property(nullable,nonatomic,weak) id<UIPickerViewDataSource> dataSource;                
@property(nullable,nonatomic,weak) id<UIPickerViewDelegate>   delegate;       

重新加载列:

- (void)reloadAllComponents;
- (void)reloadComponent:(NSInteger)component;

获取当前选中的选项序号:

- (NSInteger)selectedRowInComponent:(NSInteger)component;

指定选中的项显示在中间位置,一般设置第一项放在中间:

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;

下面是一个简单的完整示例:

#import "ViewController.h"

@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic, strong) NSArray *dayArray;
@property (nonatomic, strong) NSArray *hourArray;
@property (nonatomic, strong) NSArray *minuteArray;
@end

@implementation TimeLimitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        self.dayArray = [[NSArray alloc] initWithObjects:@"00",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30", nil];
    self.hourArray = [[NSArray alloc] initWithObjects:@"00",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23", nil];
    self.minuteArray = [[NSArray alloc] initWithObjects:@"00",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31",@"32",@"33",@"34",@"35",@"36",@"37",@"38",@"39",@"40",@"41",@"42",@"43",@"44",@"45",@"46",@"47",@"48",@"49",@"50",@"51",@"52",@"53",@"54",@"55",@"56",@"57",@"58",@"59", nil];
        self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 360, self.view.frame.size.width, 300)];
    self.pickerView.backgroundColor = [UIColor clearColor];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    [self.view addSubview:self.pickerView];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return 31;
    } else if (component == 1) {
        return 24;
    } else if (component == 2) {
        return 60;
    }
    return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        return [_dayArray objectAtIndex:row];
    } else if (component == 1) {
        return [_hourArray objectAtIndex:row];
    } else {
        return [_minuteArray objectAtIndex:row];
    }
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 35;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        self.dayString = [_dayArray objectAtIndex:row];
    } else if (component == 1) {
        self.hourString = [_hourArray objectAtIndex:row];
    } else {
        self.minuteString = [_minuteArray objectAtIndex:row];
    }
    self.combineString = [[NSString alloc] initWithFormat:@"%@天%@时%@分",_dayString,_hourString,_minuteString];
    self.timeString = _combineString;
    [self setText];
}

具体效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

waxuuuu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值