参考资料:http://www.runoob.com/ios/att-ios-ui-picker.html
lz是Ios新手,在草鸟教程学习ios控件使用,但是又有菜鸟教程的代码不给了,部分代码缺失,所以lz无法看全代码,部分功能无法实现,于是在网站找了半天(哎呀,我的暴脾气➡️发作了)终于在网站找到了lz要的功能代码,过来纪录一下
.h文件
@property(nonatomic,strong)IBOutlet UITextField *TF_departement;
@property(nonatomic,strong)IBOutlet UITextField *TF_post;
//下拉框类表
@property(nonatomic,strong)IBOutlet UIPickerView *PV_departement;
@property(nonatomic,strong)IBOutlet UIPickerView *PV_post;
@property(nonatomic,strong)NSArray *departement_list;
@property(nonatomic,strong)NSArray *post_list;
.m文件代码
self.PV_post = [[UIPickerView alloc]init];
self.PV_post.delegate = self;
self.PV_post.dataSource = self;
self.PV_post.showsSelectionIndicator = YES;
self.TF_post.delegate = self;
self.TF_post.inputView = self.PV_post;
UIButton *myButton = [[UIButton alloc] init];
[myButton setBackgroundColor:[UIColor hexstringToColor:@"0099FF"]];
myButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
[myButton setFrame:CGRectMake(0, 0, 80, 30)];
[myButton setTitle:@"Done" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(hiddenPickerView:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *sure_btn = [[UIBarButtonItem alloc] initWithCustomView:myButton];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - self.PV_departement.frame.size.height-50, 320, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
NSArray *toolbarItems = [NSArray arrayWithObjects:flexSpace,sure_btn,nil];
[toolBar setItems:toolbarItems];
self.TF_post.inputAccessoryView = toolBar;
-(void)hiddenPickerView:(id)sender{
[self.view endEditing:true];
}
//----------------------------------------------------------------UITextFieldDelegate start
-(void)textFieldDidEndEditing:(UITextField *)textField{
if([textField.text isEqualToString:@""]){
}
}
//----------------------------------------------------------------UITextFieldDelegate end
//----------------------------------------------------------------UIPickerViewDelegate start
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if(pickerView == self.PV_departement){
return self.departement_list.count;
}else{
return self.post_list.count;
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if(pickerView == self.PV_departement){
DepartEntity *depart = [self.departement_list objectAtIndex:row];
return depart.Departname;
}else{
UserRoleEntity *userRole = [self.post_list objectAtIndex:row];
return userRole.RoleName;
}
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if(pickerView == self.PV_departement){
DepartEntity *depart = [self.departement_list objectAtIndex:row];
[self.TF_departement setText:depart.Departname];
}else{
UserRoleEntity *userRole = [self.post_list objectAtIndex:row];
[self.TF_post setText:userRole.RoleName];
}
}
//----------------------------------------------------------------UIPickerViewDelegate end