Reputation: 6164
In my iPhone app, I am facing some problems related to keyboard show/hide behavior.
I have three text fields; when the third text field is clicked, I want to display a UIPickerView
and hide the keyboard for that text field. That I can do.
Now the problem is that, if the keyboard of either first or second text field is visible, and I click on the third text field, the picker becomes visible, but it appears behind the keyboard (it is only behind the keyboard of the first or second text field).
So what should I do to make the picker visible by itself and not to display any keyboard at that time?
Here is the code:-
-(void) textFieldDidBeginEditing:(UITextField *)textField{
if (textField==thirdTextField) {
[scroll setFrame:CGRectMake(00, 48, 320, 160)];
[scroll setContentSize:CGSizeMake(320,335)];
[picker setHidden:NO];
[tool1 setFrame:CGRectMake(0,180,320,44)];
[tool1 setHidden:NO];
[self.picker reloadAllComponents];
[firtTextField resignFirstResponder];
[secondTextField resignFirstResponder];
[thirdTextField resignFirstResponder];
}
else {
[scroll setFrame:CGRectMake(00, 48, 320, 200)];
[scroll setContentSize:CGSizeMake(320,335)];
[tool1 setHidden:NO];
[tool1 setFrame:CGRectMake(0,220,320,44)];
}
}
the problem is like
Upvotes: 4
Views: 2347
Reputation: 2314
Keep three textfields as member of the controller.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if(textField == 3rdTextField){
[self.firstTextField resignFirstResponder];
[self.secondTextField resignFirstResponder];
[self.thirdTextField resignFirstResponder];
}
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if(textField==3rdTextField){
[firstTextField resignFirstResponder];
[secondTextField resignFirstResponder];
}
else if(textField==secondTextField){
[firstTextField resignFirstResponder];
[3rdTextField resignFirstResponder];
}
else if(textField==firstTextField){
[secondTextField resignFirstResponder];
[3rdTextField resignFirstResponder];
}
return YES;
}
Hope this will help you.
Upvotes: 7
Reputation: 1249
Then use a Notification when keyboard becomes visible and have a boolean called isPickerVisible.
When picker becomes visible set isPickerVisible to TRUE.
In the keyboardDidShow method check whether picker is visible or not. If it is visible then hide it.
Adding a notification:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardWillShowNotification
object:nil];
And the method...
- (void)keyboardDidShow:(NSNotification*)notif {
if(isPickerVisible) {
[self hidePicker];
}
}
Hope this helped...
Upvotes: 1
Reputation: 1838
In - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField method use resignFirstResponder for all the textfield except the third textFied:-
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField==thirdTextField)
{
[firstTextField resignFirstResponder];
[secondTextField resignFirstResponder];
[textField resignFirstResponder];
[self showPickerView];
}
return YES;
}
Upvotes: 0
Reputation: 2066
use the resignFirstResponder methods and the text fields. [textField resignFirstResponder]
that will hide the keyboard.
Upvotes: 1
Reputation: 1929
Call
[yourTextField resignFirstResponder]
on all other textfields to make their keyboard disappear.
Upvotes: 3