UIPickerVIEw控件是比UIDatePicker控件更普通的Picker控件,UIDatePicker控件可以理解成是从UIPickerVIEw控件加工出来的专门进行日期选择的控件。
UIPickerVIEw控件的用法比UIDatePicker复杂一点。本文中的小例子将用UIPickerVIEw控件做出两种效果,第一个只有一个转盘,第二个有两个转盘,但这两个转盘之间没有依赖关系,也就是说改变其中一个转盘中的选择,不会对第二个转盘产生影响。在下一篇文章会做一个转盘之间有依赖关系的例子。
下图是我们的效果图:
第一个UIPickerVIEw控件可以用来选择Horse,Sheep,Pig,Dog,Cat,Chicken,Duck,Goose;第二个UIPickerVIEw在第一个基础上增加了一个转盘。
闲话少说,接下来就开始。
1、运行Xcode 4.2,新建一个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,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、运行:
总结
以上是内存溢出为你收集整理的iOS开发11:UIPickerView控件(1)全部内容,希望文章能够帮你解决iOS开发11:UIPickerView控件(1)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)