iOS App中UIPickerView选择栏控件的使用实例解析
uipickerview控件是比uidatepicker控件更普通的picker控件,uidatepicker控件可以理解成是从uipickerview控件加工出来的专门进行日期选择的控件。
uipickerview控件的用法比uidatepicker复杂一点。本文中的小例子将用uipickerview控件做出两种效果,第一个只有一个转盘,第二个有两个转盘,但这两个转盘之间没有依赖关系,也就是说改变其中一个转盘中的选择,不会对第二个转盘产生影响。在下一篇文章会做一个转盘之间有依赖关系的例子。
下图是我们的效果图:
第一个uipickerview控件可以用来选择horse,sheep,pig,dog,cat,chicken,duck,goose;第二个uipickerview在第一个基础上增加了一个转盘。
闲话少说,接下来就开始。
1、运行xcode,新建一个single view application,名称为uipickerview test1,其他设置如下图:
2、单击viewcontroller.xib,然后拖一个picker view控件到视图上:
然后再拖一个button到picker view下方,并修改名称为select:
3、在viewcontroller.h中为picker view控件创建outlet映射,名称为mypickerview,然后为select按钮创建action映射,名称为buttonpressed,具体方法不说了,可以参照上一篇文章。
4、选中picker view控件,打开connections inspector,找到delegate和datasource,从它们右边的圆圈拉线到file's owner:
5、单击viewcontroller.h,在其中添加代码:
#import <uikit/uikit.h>
@interface viewcontroller : uiviewcontroller<uipickerviewdelegate, uipickerviewdatasource>
@property (weak, nonatomic) iboutlet uipickerview *mypickerview;
@property (strong, nonatomic) nsarray *mypickerdata;
- (ibaction)buttonpressed:(id)sender;
@end
注意在@interface后面添加尖括号及其中内容,我们将viewcontroller作为picker view的delegate以及datasource。
6、代码添加:
6.1 单击viewcontroller.m,在@implementation的下一行添加代码:
@synthesize mypickerdata;
6.2 找到buttonpressed方法,添加代码如下:
- (ibaction)buttonpressed:(id)sender {
nsinteger row = [mypickerview selectedrowincomponent:0];
nsstring *selected = [mypickerdata objectatindex:row];
nsstring *msg = [[nsstring alloc] initwithformat:
@"you selected %@!", selected];
uialertview *alert = [[uialertview alloc] initwithtitle:@"hello!"
message:msg
delegate:nil
cancelbuttontitle:@"yes, i did."
otherbuttontitles:nil];
[alert show];
}
6.3 找到viewdidload方法,在其中添加代码:
- (void)viewdidload
{
[super viewdidload];
// do any additional setup after loading the view, typically from a nib.
nsarray *array = [[nsarray alloc] initwithobjects:@"horse", @"sheep", @"pig", @"dog", @"cat", @"chicken", @"duck", @"goose", nil];
self.mypickerdata = array;
}
6.4 找到viewdidunload方法,在其中添加代码:
- (void)viewdidunload
{
[self setmypickerview:nil];
[super viewdidunload];
// release any retained subviews of the main view.
// e.g. self.myoutlet = nil;
self.mypickerview = nil;
self.mypickerdata = nil;
}
6.5 在@end前面添加代码:
#pragma mark -
#pragma mark picker data source methods
- (nsinteger)numberofcomponentsinpickerview:(uipickerview *)pickerview {
return 1;
}
- (nsinteger)pickerview:(uipickerview *)pickerview numberofrowsincomponent:(nsinteger)component {
return [mypickerdata count];
}
#pragma mark picker delegate methods
- (nsstring *)pickerview:(uipickerview *)pickerview titleforrow:(nsinteger)row forcomponent:(nsinteger)component {
return [mypickerdata objectatindex:row];
}
7、运行:
上面的例子只有一个转盘,接下来我们在此基础上增加一个转盘,第一个转盘不变,第二个转盘可以选择tree,flower,grass,fence,house,table,chair,book,swing。只要添加代码就行了。
8、单击viewcontroller.h,在@interface下一行添加代码:
@property (strong, nonatomic) nsarray *mypickerdata_2;
9、单击viewcontroller.m,在其中添加代码:
9.1 在@implementation的下一行添加代码:
@synthesize mypickerdata_2;
9.2 找到viewdidload方法,在其中添加代码:
- (void)viewdidload
{
[super viewdidload];
// do any additional setup after loading the view, typically from a nib.
nsarray *array = [[nsarray alloc] initwithobjects:@"horse", @"sheep", @"pig", @"dog", @"cat", @"chicken", @"duck", @"goose", nil];
self.mypickerdata = array;
nsarray *array_2 = [[nsarray alloc] initwithobjects:@"tree", @"flower", @"grass", @"fence", @"house", @"table", @"chair", @"book",@"swing" , nil];
self.mypickerdata_2 = array_2;
}
9.3 找到viewdidunload方法,在其中追加代码:
- (void)viewdidunload
{
[self setmypickerview:nil];
[super viewdidunload];
// release any retained subviews of the main view.
// e.g. self.myoutlet = nil;
self.mypickerview = nil;
self.mypickerdata = nil;
self.mypickerdata_2 = nil;
}
9.4 找到buttonpressed方法,修改代码:
- (ibaction)buttonpressed:(id)sender {
nsinteger row = [mypickerview selectedrowincomponent:0];
nsinteger row_2 = [mypickerview selectedrowincomponent:1];
nsstring *selected = [mypickerdata objectatindex:row];
nsstring *selected_2 = [mypickerdata_2 objectatindex:row_2];
nsstring *msg = [[nsstring alloc] initwithformat:
@"you selected %@ and %@!", selected, selected_2];
uialertview *alert = [[uialertview alloc] initwithtitle:@"hello!"
message:msg
delegate:nil
cancelbuttontitle:@"yes, i did."
otherbuttontitles:nil];
[alert show];
}
9.5 找到numberofcomponentsinpickerview方法,修改其返回值为2:
- (nsinteger)numberofcomponentsinpickerview:(uipickerview *)pickerview {
return 2;
}
9.6 找到numberofrowsincomponent方法,修改其中代码:
- (nsinteger)pickerview:(uipickerview *)pickerview numberofrowsincomponent:(nsinteger)component {
if (component == 0) {
return [mypickerdata count];
}
return [mypickerdata_2 count];
}
9.7 找到下面的方法,修改代码:
- (nsstring *)pickerview:(uipickerview *)pickerview titleforrow:(nsinteger)row forcomponent:(nsinteger)component {
if (component == 0) {
return [mypickerdata objectatindex:row];
}
return [mypickerdata_2 objectatindex:row];
}
10、运行:
进阶实例
下面要用uipickerview控件做出这样的效果:它有两个转盘(component),当左边的转盘改变了选择值,右边转盘所有的选项都改变。如下图所示:
为了达到这样的效果,还是先要创建两个nsarray对象,每个转盘对应一个。然后创建一个nsdictionary对象。我们可以想象出数据是树形的,nsdictionary可以看成是一个有两列的表格,第一列存储的是关键字,每个关键字对应一个nsarray对象,这些nsarray数组中存储的是一系列的nsstring对象。
在这个例子中,第一例存储的是一些省份,第二列存储的是省份对应的地级市。
其实实现的方法跟上篇文章中的差不多,唯一不同的是要实现:改变左边转盘的选项,右边转盘内容发生相应的变化。这个功能要用到的函数我们上次也使用到了。
这次,我们先把要用到的代码写好,然后再用interface builder创建控件、实现映射等。
1、运行xcode 4.2,新建一个single view application,名称为uipickerview test2:
2、创建数据。我们用到的数据如下:
在前边的文章中曾经提到过plist文件,现在,我们就要用plist文件存储以上数据。为此,选择file — new — new file,在打开的窗口中,左边选择ios中的resource,右边选择property list:
单击next,在打开的窗口中,save as中输入名称provincecities,group选择supporting files:
单击create,就创建了provincecities.plist。然后往其中添加数据,如下图所示:
3、单击viewcontroller.h,向其中添加代码:
#import <uikit/uikit.h>
#define kprovincecomponent 0
#define kcitycomponent 1
@interface viewcontroller : uiviewcontroller <uipickerviewdelegate, uipickerviewdatasource>
@property (strong, nonatomic) iboutlet uipickerview *picker;
@property (strong, nonatomic) nsdictionary *provincecities;
@property (strong, nonatomic) nsarray *provinces;
@property (strong, nonatomic) nsarray *cities;
- (ibaction)buttonpressed;
@end
4、单击viewcontroller.m,向其中添加代码:
4.1 在@implementation下一行添加代码:
@synthesize picker;
@synthesize provincecities;
@synthesize provinces;
@synthesize cities;
4.2 在viewdidload方法中添加代码:
- (void)viewdidload
{
[super viewdidload];
// do any additional setup after loading the view, typically from a nib.
nsbundle *bundle = [nsbundle mainbundle];
nsurl *plisturl = [bundle urlforresource:@"provincecities" withextension:@"plist"];
nsdictionary *dictionary = [nsdictionary dictionarywithcontentsofurl:plisturl];
self.provincecities = dictionary;
nsarray *components = [self.provincecities allkeys];
nsarray *sorted = [components sortedarrayusingselector:@selector(compare:)];
self.provinces = sorted;
nsstring *selectedstate = [self.provinces objectatindex:0];
nsarray *array = [provincecities objectforkey:selectedstate];
self.cities = array;
}
代码中
nsbundle *bundle = [nsbundle mainbundle];
用于获得当前程序的main bundle,这个bundle可以看成是一个文件夹,其中的内容遵循特定的框架。main bundle的一种主要用途是使用程序中的资源,如图片、声音等,本例中使用的是plist文件。下面的一行
nsurl *plisturl = [bundle urlforresource:@"provincecities" withextension:@"plist"];
用来获取provincecities.plist的路径,之后将这个文件中的内容都放在一个nsdictionary对象中,用的是
nsdictionary *dictionary = [nsdictionary dictionarywithcontentsofurl:plisturl];
4.3 找到viewdidunload方法,添加代码:
- (void)viewdidunload
{
[super viewdidunload];
// release any retained subviews of the main view.
// e.g. self.myoutlet = nil;
self.picker = nil;
self.provincecities = nil;
self.provinces = nil;
self.cities = nil;
}
4.4 在@end之前添加代码,实现buttonpressed方法:
- (ibaction)buttonpressed:(id)sender {
nsinteger provincerow = [picker selectedrowincomponent:kprovincecomponent];
nsinteger cityrow = [picker selectedrowincomponent:kcitycomponent];
nsstring *province = [self.provinces objectatindex:provincerow];
nsstring *city = [self.cities objectatindex:cityrow];
nsstring *title = [[nsstring alloc] initwithformat:@"你选择了%@.", city];
nsstring *message = [[nsstring alloc] initwithformat:@"%@属于%@", city, province];
uialertview *alert = [[uialertview alloc] initwithtitle:title message:message delegate:nil cancelbuttontitle:@"好的" otherbuttontitles: nil];
[alert show];
}
4.5 在@end之前添加代码:
#pragma mark -
#pragma mark picker date source methods
- (nsinteger)numberofcomponentsinpickerview:(uipickerview *)pickerview {
return 2;
}
- (nsinteger)pickerview:(uipickerview *)pickerview numberofrowsincomponent:(nsinteger)component {
if (component == kprovincecomponent) {
return [self.provinces count];
}
return [self.cities count];
}
#pragma mark picker delegate methods
- (nsstring *)pickerview:(uipickerview *)pickerview titleforrow:(nsinteger)row forcomponent:(nsinteger)component {
if (component == kprovincecomponent) {
return [self.provinces objectatindex:row];
}
return [self.cities objectatindex:row];
}
- (void)pickerview:(uipickerview *)pickerview didselectrow:(nsinteger)row incomponent:(nsinteger)component {
if (component == kprovincecomponent) {
nsstring *selectedstate = [self.provinces objectatindex:row];
nsarray *array = [provincecities objectforkey:selectedstate];
self.cities = array;
[picker selectrow:0 incomponent:kcitycomponent animated:yes];
[picker reloadcomponent:kcitycomponent];
}
}
- (cgfloat)pickerview:(uipickerview *)pickerview widthforcomponent:(nsinteger)component {
if (component == kcitycomponent) {
return 150;
}
return 140;
}
可以看到,跟上篇文章的例子相比,大部分代码是一样的,不同的是增加了pickerview:(uipickerview *)pickerview didselectrow:(nsinteger)row incomponent:(nsinteger)component这个方法。这个方法中,当检测到修改的是左边转盘的值,则将self.cities中的内容替换成相应的数组,并执行[picker reloadcomponent:kcitycomponent];这个语句。
最后一个方法
(cgfloat)pickerview:(uipickerview *)pickerview widthforcomponent:(nsinteger)component
可以用来修改每个转盘的宽度,虽然在这个例子中不必要,但是我们得知道是怎么做的。
代码部分结束,接下来是使用interface builder添加控件、创建映射。
5、单击viewcontroller.xib,往其中添加一个uipickerview控件和一个button,按钮的名称改为“选择”,具体方法参照前面一
接下来要做的就是拉几条线。
6、选中新添加的uipickerview控件,按住control,拖到file's owner图标,在弹出菜单选择delegate和datasource:
打开assistant editor,确保其中打开的是viewcontroller.h,然后从picker属性前边的小圆圈拉线到uipickerview控件:
同样,从buttonpressed方法前边的小圆圈拉线到“选择”按钮。
7、运行:
上一篇: iPhone12预计年内销量超过8千万台
下一篇: android获取屏幕像素思路及代码