UIPickerView

一、简介

<<UIPickerView类实现对象,所谓的选择器的看法,即使用一个纺车或老虎机的比喻来显示一个或多个值集。用户可以选择旋转的车轮,使所需的行的值与选择的指

之间的对应关系值。UIDatePicker类使用一个自定义子类的UIPickerView显示日期和时间。为了看一个例子,挖掘在时钟应用程序报警窗格添加("+")按钮

<<UIPickerView是一个选择器控件,它比UIDatePicker更加通用,它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活

<<继承关系:UIPickerView --> UIView -->UIResponder-->NSObject

<<UIPickerView直接继承了UIView,没有继承UIControl,因此,它不能像UIControl那样绑定事件处理方法,UIPickerView的事件处理由其委托对象完成。使用UIPickerView的对象应该遵守UIPickerViewDataSource,UIPickerViewDelegate。 

格式为

1--> 设置数据源(属性的作用

[pickerView setDelegate:self];   (这是具体的例子

@property(nullable,nonatomic,weak) id dataSource;// 设置数据源, 默认是空的   (这是属性的说明

二、UIPickerView的属性(属性的顺序与苹果API一致)

1-->设置数据源

 [pickerView setDelegate:self]; 

@property(nullable,nonatomic,weak) id dataSource;// 设置数据源, 默认是空的,弱引用

2-->设置代理

[pickerView setDelegate:self];

@property(nullable,nonatomic,weak) id delegate; //设置代理, 默认是空的,弱引用

3-->设置是否显示UIPickerView中的选中标记,在iOS7之后这个属性没有任何效果

pickerView.showsSelectionIndicator =YES;

@property(nonatomic) BOOL showsSelectionIndicator; // 默认是 NO。该属性控制是否显示UIPickerView中的选中标记(以高亮背景作为选中标记)

4-->获取UIPickerView指定列中包含的列表项的数量

NSInteger row1 =pickerView.numberOfComponents; //获取组键数(列数)

@property(nonatomic,readonly) NSInteger numberOfComponents; /从数据源和委托获取并缓存的信息

三、UIPickerView的属性方法

1-->获取UIPickerView包含的列数量

 NSInteger row2 = [pickerView numberOfRowsInComponent:1];//获取对象下标组键(列数)的行数

- (NSInteger)numberOfRowsInComponent:(NSInteger)component;

2--> 获取UIPickerView包含的指定列中列表项的大小。该方法返回一个CGSize对象。

 NSLog(@"%@", NSStringFromCGSize( [self.pickerView rowSizeForComponent:0])); //打印获取的每列的size;

- (CGSize)rowSizeForComponent:(NSInteger)component;

3--> 返回该UIPickerView指定列的列表项所使用的UIView控件。

UIView *pickview=[self.pickerView viewForRow:0 forComponent:0];

- (nullable UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;//返回视图提供的委托通过pickerView:viewForRow:forComponent:reusingView:

如果行或者组件不可见或委托没有实现pickerView:viewForRow:forComponent:reusingView,则为nil

4--> 重载所有分区

[pickerView reloadAllComponents];//刷新UIPickerView

- (void)reloadAllComponents;

5--> 重载某一分区

[pickerView reloadComponent:1];//刷新某一列的数据

- (void)reloadComponent:(NSInteger)component;

6--> 设置选中该UIPickerView中指定列的特定列表项

[pickerView pickerView selectRow:2 inComponent:0animated:YES]; //设置选中的第几列第几行 ,程序一开始运行时就选中该列该行

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated; // 将指定的行滚动到中心。该方法设置选中该UIPickerView中指定列的特定列表项。最后一个参数控制是否使用动画。

7--> 返回该UIPickerView指定列中被选中的列表项。

self.row =  [self.pickerView selectedRowInComponent:0];//获取被选中的第一列的行数(这里是为了在给第二列赋值时用的,因此在此之前要理清代理方法的执行顺序,以免造成不必要的bug)

- (NSInteger)selectedRowInComponent:(NSInteger)component; //返回选定行。如果没有选中返回-1

四、UIPickerView的UIPickerViewDataSource

必须实现的方法

1-->返回显示的列数

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView

{

    return1;// 返回1表明该控件只包含1列

}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;//UIPickerViewDataSource中定义的方法,该方法的返回值决定该控件包含的列数

2-->显示每组键(列数)的行数

//指定每个表盘上有几行数据

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

{

    NSInteger result = 0;

    switch (component) {

        case 0:

            result = self.letter.count;//根据数组的元素个数返回几行数据

            break;

        case 1:

            result = self.number.count;

            break;


        default:

            break;

    }

    return result;

}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;       //指定每个表盘上有几行数据

五、UIPickerView的UIPickerViewDelegate

可选方法

1-->返回每列的宽度

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{

 if (component ==0) {

 return 100;//第一组键(列)的宽度

}else{

 return 100;//第二组键(列)的宽度

    }

}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component __TVOS_PROHIBITED;//该方法返回的CGFloat值将作为该UIPickerView控件中指定列的宽度

2-->返回每列的高度

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{

 if (component ==0) {

 return 100;//第一组键(列)的高度

}else{

 return 100;//第二组键(列)的高度

    }

}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component __TVOS_PROHIBITED;//该方法返回的CGFloat值将作为该UIPickerView控件中指定列中列表项的高度。

3-->设置某一行显示的标题

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

 if (component ==0) {

 return [self.content[row]objectForKey:@"State"];//返回省的名称

}else{

 //下面俩行代码是获得plist文件中城市的名称

 NSArray *cities = [self.content[self.row]objectForKey:@"Cities"];

 NSString *city =[cities[row] objectForKey:@"city"];

return city;

    }

}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED;//该方法返回的UIView控件将直接作为该UIPickerView控件中指定列的指定列表项。

4-->通过属性字符串设置某一行显示的标题

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{

 NSString *str = [_nameArray objectAtIndex:row];

 NSMutableAttributedString *AttributedString = [[NSMutableAttributedStringalloc]initWithString:str];

[AttributedString addAttributes:@{NSFontAttributeName:[UIFontboldSystemFontOfSize:18], NSForegroundColorAttributeName:[UIColor whiteColor]}range:NSMakeRange(0, [AttributedString length])];

 return AttributedString;

}

- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;

<注意>如果以上两个方法都实现了,优先实现attributedTitleForRow的方法

5-->设置某一行显示的view视图

//可以用UILabel 来设置字体大小 和背景颜色 是一个可以自定义设置的方法

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView*)view{

 UILabel *label;//声明一个UILabel

 if (component == 0) {//当是第一行

 //设置frame

label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];

 //将省名赋给label.text

label.text = [self.content[row]objectForKey:@"State"];

 //改变字体的颜色

label.textColor = [UIColor yellowColor];

 //设置字体大小

label.font = [UIFont systemFontOfSize:20];//用label来设置字体大小

 //改变背景的颜色

        label.backgroundColor = [UIColor redColor];

}else if (component == 1){//当是第二行时


label  = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 100, 50)];


 NSArray *cities = [self.content[self.row]objectForKey:@"Cities"];

 NSString *city =[cities[row] objectForKey:@"city"];


label.text = city ;//将城市名称赋给label.text

label.textColor = [UIColor purpleColor];

label.font = [UIFont systemFontOfSize:20];


        label.backgroundColor = [UIColor redColor];

    }

 return label;


}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view __TVOS_PROHIBITED;//当用户单击选中该UIPickerView控件的指定列的指定列表项时将会激发该方法。

6-->选中某一行时执行的回调

//被选中的第几列 的 第几行

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

 if(component ==0){

 //刷新对应列

        [self.pickerView reloadComponent:1];

 //当选择第一列时 第二列切到第一行

[self.pickerView selectRow:0inComponent:component+1 animated:YES];

    }

}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component __TVOS_PROHIBITED;//当用户单击选中该UIPickerView控件的指定列的指定列表项时将会激发该方法

六、UIPickerView的拓展

实际应用中UIPickerView经常和UITextField配合使用,给大家提供一个很方便的封装类YLSOPickerView

调用方法 

1、导入#import "YLSOPickerView.h"

2、在- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField实现

#pragma mark UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

    if (textField.tag==401) {

        YLSOPickerView *picker = [[YLSOPickerView alloc]init];

        picker.array = @[@"中信银行",@"中国银行",@"中国邮政储蓄银行",@"上海浦东发展银行",@"广发银行股份有限公司",@"华夏银行",@"中国农业银行",@"中国工商银行",@"中国建设银行",@"招商银行",@"中国民生银行",@"兴业银行",@"中国光大银行",@"交通银行"];

        picker.title = bank;

        [picker show];

        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];

        //设置成NO表示当前控件响应后会传播到其他控件上,默认为YES。

        tapGestureRecognizer.cancelsTouchesInView = YES;

        //将触摸事件添加到当前view

        [picker addGestureRecognizer:tapGestureRecognizer];

        return NO;


    }

    return YES;

}

3、实现声明手势注销方法

- (void)keyboardHide:(UITapGestureRecognizer *)tap

{

    YLSOPickerView *pickView=(YLSOPickerView *)tap.view;//最好用单例

    [pickView quit];


}

4、-viewDidload里面声明通知

- (void)viewDidLoad

{

  [super viewDidLoad];

  //接受通知

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getValue:) name:@"value" object:nil];

}

5、实现通知方法

-(void)getValue:(NSNotification *)notification//点击完成输出选中的text

{

self.textFeild.text = notification.object;

    DLog(@"%@", self.textFeild.text);

}

参考:

iOS系统UIPickerView的简单使用和方法属性介绍

UIPickerView

iOS学习 - UIPickerView

iOS选择器视图控件(UIPickerView)使用方法总结

YLSPicker

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269