开发者

Problem showing/hiding keyboard in iPhone app

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)];
      [to开发者_如何学Gool1 setHidden:NO];
      [tool1 setFrame:CGRectMake(0,220,320,44)];
}
}

the problem is like

Problem showing/hiding keyboard in iPhone app


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.


Call

[yourTextField resignFirstResponder]

on all other textfields to make their keyboard disappear.


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜