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

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com