iOS-自己定義鍵盤選擇器
阿新 • • 發佈:2017-07-03
ive 鍵盤 tac spa 人民幣 nco ger num 指示
目標樣式:
直接上代碼:
遵守協議
<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 @"..";
}
}
iOS-自己定義鍵盤選擇器