0

I have some UITextField on the view, which will bring up the keyboard when it's tapped. There's a button which will navigate to another view also. Theres a problem when I navigate from this view to the 2nd view with a keyboard being active. When I navigate back from the 2nd view, the keyboard will appear on its own. How do I prevent this?

   -(IBAction) loginButton:(id) sender
    {
        [currentTextField resignFirstResponder];
        RequestPage *RequestPageview = [[RequestPage alloc] initWithNibName:nil bundle:nil];


        [UIView beginAnimations:@"flipping view" context:nil];
        [UIView setAnimationDuration:1];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp
                               forView:self.view.superview
                                 cache:YES];

        [UIView commitAnimations];
        [self presentModalViewController:RequestPageview animated:YES];
        //ß[self.view addSubview:RequestPageview.view];
    }
//---when the keyboard appears---
-(void) keyboardDidShow:(NSNotification *) notification {
    if (keyboardIsShown) return;

    NSDictionary* info = [notification userInfo];

    //---obtain the size of the keyboard---
    NSValue *aValue =
    [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect =
    [self.view convertRect:[aValue CGRectValue] fromView:nil];


    //---resize the scroll view (with keyboard)---
    CGRect viewFrame = [scrollview frame];
    NSLog(@"%f", viewFrame.size.height);
    viewFrame.size.height -= keyboardRect.size.height;
    scrollview.frame = viewFrame;
    NSLog(@"%f", keyboardRect.size.height);
    NSLog(@"%f", viewFrame.size.height);
    //---scroll to the current text field---
    CGRect textFieldRect = [currentTextField frame];
    [scrollview scrollRectToVisible:textFieldRect animated:YES];
    keyboardIsShown = YES;
        NSLog(@"Login Keyboard appear");
}

//---when the keyboard disappears---
-(void) keyboardDidHide:(NSNotification *) notification {
    NSDictionary* info = [notification userInfo];

    //---obtain the size of the keyboard---
    NSValue* aValue =
    [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect =
    [self.view convertRect:[aValue CGRectValue] fromView:nil];


    //---resize the scroll view back to the original size
    // (without keyboard)---
    CGRect viewFrame = [scrollview frame];
    viewFrame.size.height += keyboardRect.size.height;
    scrollview.frame = viewFrame;

    keyboardIsShown = NO;
        NSLog(@"Login Keyboard disappear");
}


2011-05-27 16:57:20.628 LoginPage[322:207] Login view appear // loaded the app
2011-05-27 16:57:32.220 LoginPage[322:207] Login Keyboard appear // tap on textfield
2011-05-27 16:57:35.665 LoginPage[322:207] Request view appeared // navigate to second view with keyboard shown
2011-05-27 16:57:35.667 LoginPage[322:207] Login view disappear
2011-05-27 16:57:35.978 LoginPage[322:207] Request Keyboard disappear // weird? I should have hide the Login Keyboard instead
2011-05-27 16:57:39.738 LoginPage[322:207] Login view appear // navigate back
2011-05-27 16:57:39.740 LoginPage[322:207] Request view disappeared 

2 Answers 2

1

If you want to hide keyboard on tap of a button and if you have more than one text fields then you can use this code...

[self.view endEditing:YES];

Tap any where on view, and keyboard will dissappear...

Enjoy !!...

0

In the loginButton method, call resignFirstResponder on the text field that's being edited. When the user navigates back to that view controller the keyboard will no longer be present. You'll need to change your code slightly so you have a reference to the text field. If there is only one text field, an IBOutlet will do. For multiple fields, make your view controller the delegate and you can keep a reference to the current text field when the user begins editing. You can also traverse your view's subviews, although this is potentially not as efficient.

1
  • @March Charbonneau Thanks for the reply. I managed to get the keyboard to hide when it changes view but with some weird behavior. I had the keyboardDidHide & keyboardDidShow method on both pages for performing some stuffs accordingly to the status of the keyboard. It seems that when the keyboard is active on the 1st view and then if I navigates to the second view it calls the KeyboardDidHide method on the second view instead of the 1st. Likewise when the keyboard is shown on the second view and navigating it back to view 1 will call the keyboardDidHide of the 1st view instead of the second.
    – Gavin
    May 27, 2011 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.