为键盘切换 UIPicker
Posted
技术标签:
【中文标题】为键盘切换 UIPicker【英文标题】:Switching a UIPicker for a keybaord 【发布时间】:2013-08-16 22:25:44 【问题描述】:我对此进行了研究,但只发现了如何将键盘切换为键盘。现在我有一个 UIPicker,它会在点击文本字段时出现。在 Picker 中有三个选项,第三个是自定义选项。我要发生的是,选择自定义时,Uipicker将用键盘替换。我猜这将通过if (select == 2)
方法完成。我不需要一大堆代码,只需要替换 Picker 的方法。另外我想制作一个带有后退按钮的工具栏来返回选择器。我目前有一个带有 doneButton 的工具栏来释放选择器,但我需要在键盘出现时出现一个按钮,并且该按钮能够将键盘切换回我的选择器。
到目前为止,我的选择器、工具栏和文本字段是:
...
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *array = [[NSArray alloc] initWithObjects:@"...",@"...",@"Custom", nil];
self.PickerData = array;
...
UIToolbar *toolBar = [[UIToolbar alloc] init];
toolBar.barStyle = UIBarStyleBlackOpaque;
[toolBar sizeToFit];
...
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(releasePicker)];
Picker = [[UIPickerView alloc] init];
Picker.showsSelectionIndicator = YES;
Picker.delegate = self;
doneButton.image = [UIImage imageNamed:@"button.png"];
[toolBar setItems:@[flexSpace, doneButton] animated:YES];
self.habitField.inputAccessoryView = toolBar;
[self.habitField setEnabled: YES];
[self.habitField setInputView:Picker];
- (void)releasePicker
[self.habitField resignFirstResponder];
...
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
return 1;
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
return [PickerData count];
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
return [self.PickerData objectAtIndex:row];
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
int select = row;
if (select == 0)
...
if (select == 1)
...
if (select == 2)
...
@end
【问题讨论】:
【参考方案1】:if (select == 2)
[self.habitField resignFirstResponder];
[self.habitField setInputView:nil];
[self.habitField becomeFirstResponder];
这基本上是隐藏 UIPickerView 然后删除它然后弹出键盘。
如果您想要 UIPicker 升级,请使用以下代码:
[self.habitField resignFirstResponder];
[self.habitField setInputView:PickerData];
[self.habitField becomeFirstResponder];
【讨论】:
我怎样才能让按钮把选择器带回来?以上是关于为键盘切换 UIPicker的主要内容,如果未能解决你的问题,请参考以下文章