目标样式:
直接上代码:
遵守协议
<UIPickerViewDataSource,UIPickerViewDelegate>
实现方法
//创建 UITextField 设置setInputAccessoryView 为自己定义Toolbar 而且将键盘样式更改为键盘选择器
-(void)creatTextField{
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 64, [UIScreen mainScreen].bounds.size.width - 20, 44)];
[textField setInputAccessoryView:self.kbToolbar];
//自己定义键盘选择器
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, kScreenWidth, 320)];
[pickerView setDataSource:self];
[pickerView setDelegate:self];
//选择指示器
[pickerView setShowsSelectionIndicator:YES];
//键盘类型自己定义
[self.sourceFD setInputView:pickerView];
[textField setInputView:pickerView];
[self.view addSubview:textField];
}
实现数据源和代理方法
#pragma mark - UIPickerViewDelegate 和 UIPickerViewDataSource
//必须实现
// returns the number of 'columns' to display. ->选择器一共同拥有多少列!
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
// returns the # of rows in each component.. -> 选择器每列有多少行!
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return 5;
}
//选择器每行名称
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (row == 0) {
return @"人民币(RMB)";
}else if (row == 1){
return @"美元(USD)";
}else if (row == 2){
return @"英镑(GBP)";
}else if (row == 3){
return @"日元(JPY)";
}else if (row == 4){
return @"欧元(EUR)";
}else{
return @"..";
}
}