IOS UIPickerView+UITextField_ToolBar

参考资料: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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值