iOS用UIPickerView替换键盘然后返回键盘

时间:2016-10-26 15:21:07

标签: ios keyboard uipickerview uikeyboard

我的应用中有一个文本字段,用户可以在其中选择他们所属的组以及其他几个字段。当他们到达Group TextField时,它会弹出一个UIPickerView,它有几个已经创建的选项,以及一个选项" Create New Group"。选择该选项后,我希望UIPickerView消失,让键盘弹出,这样他们就可以再次输入。我可以让选择器视图出现并消失,但无法恢复键盘。到目前为止,这是代码。

-(void)addPickerView{

     __block NSMutableArray *arr = [[NSMutableArray alloc] init];

    PFQuery *rejectedNumber = [PFQuery queryWithClassName:@"Group"];
    [rejectedNumber orderByAscending:@"GroupName"];
    [rejectedNumber findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {



        if (!objects) {
            // Did not find any UserStats for the current user

            NSLog(@"NotFound");
        } else {
            pickerArray = objects;
            [myPickerView reloadAllComponents];

        }
    }];

     //pickerArray = GoGo;
    self.theView.signUpView.additionalField.delegate = self;
    [self.theView.signUpView.additionalField setPlaceholder:@"Choose Group"];
    myPickerView = [[UIPickerView alloc]init];
    myPickerView.dataSource = self;
    myPickerView.delegate = self;
    myPickerView.showsSelectionIndicator = YES;
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Done" style:UIBarButtonItemStyleDone
                                   target:self action:@selector(finishIt)];
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:
                          CGRectMake(0, self.view.frame.size.height-
                                     self.theView.signUpView.additionalField.frame.size.height-50, 320, 50)];
    [toolBar setBarStyle:UIBarStyleBlackOpaque];
    NSArray *toolbarItems = [NSArray arrayWithObjects:
                             doneButton, nil];
    [toolBar setItems:toolbarItems];
    self.theView.signUpView.additionalField.inputView = myPickerView;
    self.theView.signUpView.additionalField.inputAccessoryView = toolBar;

}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
    return [pickerArray count] + 1;
}

#pragma mark- Picker View Delegate

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component{
    if (row == 0)
        [self.theView.signUpView.additionalField.inputView removeFromSuperview];
        [self.theView.signUpView.additionalField becomeFirstResponder];

    }
    else {

    [self.theView.signUpView.additionalField setText:self.theGroup];
    }
}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *groupName  = @"Create New Group";
    if (row != 0)
    {
        PFObject *object = pickerArray[row - 1]; // -1 to handle the array index
        self.theGroup    = object[@"GroupName"];
        groupName        = object[@"GroupName"];
    }
    return groupName;
}

1 个答案:

答案 0 :(得分:1)

我注意到的第一件事是,如果条件:

,你在这里错过了一个括号
if (row == 0)
    [self.theView.signUpView.additionalField.inputView removeFromSuperview];
    [self.theView.signUpView.additionalField becomeFirstResponder];

}

而是使用它:

if (row == 0) {
    [self.theView.signUpView.additionalField resignFirstResponder];
    self.theView.signUpView.additionalField.inputView = nil;
    [self.theView.signUpView.additionalField becomeFirstResponder];
}