uipickerview控件在给用户选择某些特定的数据时经常使用到,这里演示一个简单的选择数据,显示在uitextfield输入框里,把uipickerview作为输入view,用toolbar作为选定数据的按钮。和其他uitableview控件相似,uipickerview也需要数据源。
我们要实现的效果如下:
下面开始使用的步骤。
1、打开xcode 4.3.2,新建一个single view application ,命名为pickerviewdemo,company identifier 为:com.rongfzh.yc
2、拖放控件
2.1、拖放一个uipickerview,放置在view的最下方
2.2、拖放一个toolbar控件,放置在view的外面,让它不属于view的子控件,并把item命名为“完成”,效果如下:
2.3 放置一个flexible space bar button item 撑开
2.4 放一个uitextfield,用来显示选择的数据
3、创建映射
在viewcontroller.xib文件里按 alt + command+ enter键,打开assistant editor,按住control键,选择各个控件,拖拽到 viewcontroller.h文件里,生成以下变量代码
#import <uikit/uikit.h>
@interface viewcontroller : uiviewcontroller
{
nsarray *pickerarray;
}
- (ibaction)selectbutton:(id)sender;
@property (strong, nonatomic) iboutlet uitoolbar *donetoolbar;
@property (strong, nonatomic) iboutlet uipickerview *selectpicker;
@property (strong, nonatomic) iboutlet uitextfield *textfield;
@end
4、实现数据源和协议
viewcontroller.h文件里实现
<uipickerviewdelegate, uitextfielddelegate,uipickerviewdatasource>
#import <uikit/uikit.h>
@interface viewcontroller : uiviewcontroller<uipickerviewdelegate, uitextfielddelegate,uipickerviewdatasource>
{
nsarray *pickerarray;
}
- (ibaction)selectbutton:(id)sender;
@property (strong, nonatomic) iboutlet uitoolbar *donetoolbar;
@property (strong, nonatomic) iboutlet uipickerview *selectpicker;
@property (strong, nonatomic) iboutlet uitextfield *textfield;
@end
viewcontroller.m文件
-(nsinteger)numberofcomponentsinpickerview:(uipickerview *)pickerview{
return 1;
}
-(nsinteger) pickerview:(uipickerview *)pickerview numberofrowsincomponent:(nsinteger)component{
return [pickerarray count];
}
-(nsstring*) pickerview:(uipickerview *)pickerview titleforrow:(nsinteger)row forcomponent:(nsinteger)component{
return [pickerarray objectatindex:row];
}
-(void)textfielddidendediting:(uitextfield *)textfield{
nsinteger row = [selectpicker selectedrowincomponent:0];
self.textfield.text = [pickerarray objectatindex:row];
}
上面numberofcomponentsinpickerview返回有几个pickerview ,
textfielddidendediting这个在textfield结束编辑时,显示pickerview选择中的数据。
toolbar 的item的完成按钮
- (ibaction)selectbutton:(id)sender {
[textfield endediting:yes];
}
5、初始化
- (void)viewdidload
{
[super viewdidload];
pickerarray = [nsarray arraywithobjects:@"动物",@"植物",@"石头",@"天空", nil];
textfield.inputview = selectpicker;
textfield.inputaccessoryview = donetoolbar;
textfield.delegate = self;
selectpicker.delegate = self;
selectpicker.datasource = self;
selectpicker.frame = cgrectmake(0, 480, 320, 216);
}
代码解释:
设置委托
textfield.delegate =self;
selectpicker.delegate =self;
selectpicker.datasource =self;
隐藏uipickerview
selectpicker.frame =cgrectmake(0,480,320, 216);
运行:
uipickerview控件的关联选择
当选择左边的一级选项时,左边展示一级选项里含有的二级选项,选择后显示在textfield里。
如何实现呢?建立一个和左边的列表key对应的数组,当选择这个key时,刷新左边uipickerview部分的内容显示对应数组的数据,选择时,找到
两个uipickerview部件rowindex,找出数据,放到textfield里。
1、打开上篇pickerviewdemo项目,在viewcontroller.h添加两个成员变量:nsarray *subpickerarray; nsdictionary *dicpicker;
#import <uikit/uikit.h>
@interface viewcontroller : uiviewcontroller<uipickerviewdelegate, uitextfielddelegate,uipickerviewdatasource>
{
nsarray *pickerarray;
nsarray *subpickerarray;
nsdictionary *dicpicker;
}
- (ibaction)selectbutton:(id)sender;
@property (strong, nonatomic) iboutlet uitoolbar *donetoolbar;
@property (strong, nonatomic) iboutlet uipickerview *selectpicker;
@property (strong, nonatomic) iboutlet uitextfield *textfield;
@end
2、初始化
- (void)viewdidload
{
[super viewdidload];
pickerarray = [nsarray arraywithobjects:@"动物",@"植物",@"石头", nil];
dicpicker = [nsdictionary dictionarywithobjectsandkeys:
[nsarray arraywithobjects:@"鱼",@"鸟",@"虫子", nil], @"动物",
[nsarray arraywithobjects:@"花",@"草",@"葵花", nil], @"植物",
[nsarray arraywithobjects:@"疯狂的石头",@"花岗岩",@"鹅卵石", nil], @"石头",nil];
subpickerarray = [dicpicker objectforkey:@"动物"];
textfield.inputview = selectpicker;
textfield.inputaccessoryview = donetoolbar;
textfield.delegate = self;
selectpicker.delegate = self;
selectpicker.datasource = self;
selectpicker.frame = cgrectmake(0, 480, 320, 216);
}
给 nsdictionary *dicpicker;赋值,对应的三个关键字添加了对应的数组。
3、component返回两个,这样就有两个齿轮了。
-(nsinteger)numberofcomponentsinpickerview:(uipickerview *)pickerview{
return 2;
}
4、使用宏
在#import "viewcontroller.h"下面定义两个宏,代表uipickerview齿轮的左边的部分和右边的部分。左边的部分是0,右边的是1.
#import "viewcontroller.h"
#define kfirstcomponent 0
#define ksubcomponent 1
5、判断是那个齿轮,返回相应的数据的count。
-(nsinteger) pickerview:(uipickerview *)pickerview numberofrowsincomponent:(nsinteger)component{
if(component == kfirstcomponent){
return [pickerarray count];
}else {
return [subpickerarray count];
}
}
6、根据component返回相应的string数据
-(nsstring*) pickerview:(uipickerview *)pickerview titleforrow:(nsinteger)row forcomponent:(nsinteger)component{
if(component == kfirstcomponent){
return [pickerarray objectatindex:row];
}else {
return [subpickerarray objectatindex:row];
}
}
7、拖动左边的齿轮时,右边的数据相应的reload更新。
-(void)pickerview:(uipickerview *)pickerview didselectrow:(nsinteger)row incomponent:(nsinteger)component{
if (component == kfirstcomponent) {
subpickerarray = [dicpicker objectforkey:[pickerarray objectatindex:row]];
[pickerview selectrow:0 incomponent:ksubcomponent animated:yes];
[pickerview reloadcomponent:ksubcomponent];
}
}
8、相应选择的数据,并显示在textfield上。
-(void)textfielddidendediting:(uitextfield *)textfield{
nsinteger firstviewrow = [selectpicker selectedrowincomponent:kfirstcomponent];
nsinteger subviewrow = [selectpicker selectedrowincomponent:ksubcomponent];
nsstring * firststring = [pickerarray objectatindex:firstviewrow];
nsstring * substring = [[dicpicker objectforkey:[pickerarray objectatindex:firstviewrow]] objectatindex:subviewrow] ;
nsstring *textstring = [[nsstring alloc ] initwithformat:@"您选择了:%@%@%@", firststring, @" 里的 ", substring];
self.textfield.text = textstring;
}
- (ibaction)selectbutton:(id)sender {
[textfield endediting:yes];
}
大功告成,运行,点击textfield,弹出: