4

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

enter image description here

ios
  • 6,134
  • 20
  • 71
  • 103

5 Answers5

7

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.

Moshe
  • 57,511
  • 78
  • 272
  • 425
Adarsh V C
  • 2,314
  • 1
  • 20
  • 37
  • Thanks for reply but this didn"t work i am not able to hide keyboard in front of my pickerview – ios Apr 01 '11 at 09:18
3

Call

[yourTextField resignFirstResponder]

on all other textfields to make their keyboard disappear.

Erik S
  • 1,939
  • 1
  • 18
  • 44
1

use the resignFirstResponder methods and the text fields. [textField resignFirstResponder] that will hide the keyboard.

Jorge
  • 2,056
  • 1
  • 16
  • 23
1

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...

SNR
  • 1,249
  • 2
  • 20
  • 38
  • -1: The OP said that part is already working: ...display a UIPickerView and hide the keyboard for that text field. That I can do. – jscs Apr 01 '11 at 08:03
0

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;
 }
Javal Nanda
  • 1,828
  • 3
  • 14
  • 26