user717452 user717452 - 1 month ago 10
iOS Question

iOS Replace Keyboard with UIPickerView then Back to Keyboard

I have a textfield in my app in which a user can choose a Group they belong to, along with several other fields. When they get to the Group TextField, it pops up a UIPickerView that has several choices that have already been created, along with an option to "Create New Group". When that option is chosen, I want the UIPickerView to go away, and have the keyboard pop back up so they can type again. I can get the picker view to appear and to go away, but can't get the keyboard back. Here is code so far.

-(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;
}

Answer

First thing i noticed is, you missed a braces here after if condition:

if (row == 0)
    [self.theView.signUpView.additionalField.inputView removeFromSuperview];
    [self.theView.signUpView.additionalField becomeFirstResponder];

}

Instead use this:

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