(1)效果
(2)先在storyboard中设计界面,然后源码(直接在ViewController中码)
#import "ViewController.h"
@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (nonatomic,strong) NSArray *foods;
@property (weak, nonatomic) IBOutlet UITextField *fruitText;
@property (weak, nonatomic) IBOutlet UITextField *MainfoodText;
@property (weak, nonatomic) IBOutlet UITextField *BeverageText;
@property (strong,nonatomic) UIPickerView *foodPicker;
@property (weak, nonatomic) IBOutlet UITextField *dateText;
@property(strong,nonatomic) UIDatePicker *datePicker;
@end
@implementation ViewController
- (void)viewDidLoad {
_foodPicker=[[UIPickerView alloc]init];
_foodPicker.dataSource=self;
_foodPicker.delegate=self;
self.fruitText.inputView=_foodPicker;
self.MainfoodText.inputView=_foodPicker;
self.BeverageText.inputView=_foodPicker;
UIToolbar *foodToolBar=[[UIToolbar alloc]init];
foodToolBar.barTintColor=[UIColor grayColor];
foodToolBar.frame=CGRectMake(0, 0, 320, 38);
UIBarButtonItem *randomBtn=[[UIBarButtonItem alloc]initWithTitle:@"随机" style:UIBarButtonItemStylePlain target:self action:@selector(randomPicker)];
[randomBtn setTintColor:[UIColor whiteColor]];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"完毕" style:UIBarButtonItemStylePlain target:self action:@selector(doneClick)];
[doneBtn setTintColor:[UIColor whiteColor]];
UIBarButtonItem *spaceBtn=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
foodToolBar.items=@[randomBtn,spaceBtn,doneBtn];
self.fruitText.inputAccessoryView=foodToolBar;
self.MainfoodText.inputAccessoryView=foodToolBar;
self.BeverageText.inputAccessoryView=foodToolBar;
_datePicker=[[UIDatePicker alloc]init];
_datePicker.datePickerMode=UIDatePickerModeDate;
_datePicker.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
UIToolbar *dateToolBar=[[UIToolbar alloc]init];
UIBarButtonItem *doneBtn1=[[UIBarButtonItem alloc]initWithTitle:@"完毕" style:UIBarButtonItemStylePlain target:self action:@selector(dateDoneClick)];
dateToolBar.barTintColor=[UIColor grayColor];
dateToolBar.frame=CGRectMake(0, 0, 320, 38);
dateToolBar.items=@[spaceBtn,doneBtn1];
self.dateText.inputView=_datePicker;
self.dateText.inputAccessoryView=dateToolBar;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)randomPicker{
for (int i=0; i<self.foods.count; i++) {
int row=arc4random()%[self.foods[i] count];
[self.foodPicker selectRow:row inComponent:i animated:YES];
[self pickerView:nil didSelectRow:row inComponent:i];
}
}
-(void)doneClick{
[self.view endEditing:YES];
for (int i=0; i<self.foods.count; i++) {
[self pickerView:nil didSelectRow:[self.foodPicker selectedRowInComponent:i] inComponent:i];
}
}
-(void)dateDoneClick{
[self.view endEditing:YES];
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"YYYY-MM-dd"];
NSString *newDate=[formatter stringFromDate:self.datePicker.date];
self.dateText.text=[NSString stringWithFormat:@"%@",newDate];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return self.foods.count;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [self.foods[component] count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return self.foods[component][row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (component==0) {
self.fruitText.text=self.foods[component][row];
}else if (component==1){
self.MainfoodText.text=self.foods[component][row];
}else{
self.BeverageText.text=self.foods[component][row];
}
}
-(NSArray *)foods{
if (_foods==nil) {
_foods=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"foods.plist" ofType:nil]];
}
return _foods;
}
@end
(3)总结
——这些选择器,基本都是结合输入框来使用的,即代替原本的键盘。所以我们的设置主要是用到了输入框的inputView属性。
——并且这些弹出的选择器一般上面会有一个UIToolBar,这里用到的是inputAccessoryView属性。
——并且这个UIToolBar里面放得都是UIBarButtonItem元素。我们能够自己定义也能够使用默认的,最重要的是UIBarButtonSystemItemFlexibleSpace用来对齐布局的。
——这里用的UIPickerView仅仅是显示字符串。而有的则须要显示更高级的东西,所以就须要用到view。而不是title。
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
上面假设用到view作为UIPickerView内容,它也被设计成缓冲池回收机构。