切换键盘的UIPicker

时间:2013-08-16 22:25:45

标签: iphone ios uitextfield uikeyboard uipicker

我研究了这个,但发现只有如何为键盘切换键盘。现在我有一个UIPicker,当点击文本字段时会出现。在Picker中他们有三个选项,第三个是自定义。我想要发生的是当选择自定义时,用键盘替换UIPicker。我猜测这将使用if (select == 2) { }方法完成。我不需要一大堆代码,只需要替换Picker的方法。此外,我想制作一个带有后退按钮的工具栏,以回到选择器。我目前有一个带有doneButton的工具栏来释放选择器,但是当键盘出现并且按钮能够将键盘切换回我的Picker时,我需要显示一个按钮。

到目前为止,我的选择器,太多和文本字段是:

...

- (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 个答案:

答案 0 :(得分: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];