iOS-自定义键盘选择器

目标样式:

直接上代码:
遵守协议

<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 @"..";
    }
}

你可能感兴趣的:(选择器,UITextField,自定义键盘)