LuisSedano LuisSedano - 2 years ago 106
Objective-C Question

Keyboard appearing briefly when popping viewcontroller iOS

I'm experimenting an issue with the numeric pad when popping the current UIViewController from the UINavigationController.

In my current UIViewController. I have a few UITextField and a "Save" button in the UINavigationBar. The expected behavior is as follows:

When the user taps "Save", the keyboard must hide and a network operation is performed. In its callback, an UIAlertView is shown. When user dismisses this UIAlertView, a notification raises and the current UIViewController performs

[self.navigationController popViewControllerAnimated:YES];

The thing is, if "Save" is pressed with the keyboard still showing, after performing the popViewControllerAnimated, the keyboard appears briefly and from left to right (as if it was visible in the previous UIViewController).

I've tried

[myTextField resignFirstResponder]

when user taps "Save", when user dismisses the UIAlertView, and even in the


method. Some other answers suggest using

[self.view endEditing:YES];

but it doesn't work either.

If I could use the regular keyboard it would be trivial to override

- (BOOL)textFieldShouldReturn:(UITextField *)textField
[textField resignFirstResponder];

return YES;

to hide it when user taps "Return", "Done", etc. But being the numeric pad doesn't allow you to show that "finish" button.

I'd appreciate any help, thank you all for your time

Answer Source

Try this:

Set the text field delegate and its return type as Done and pad as numeric pad.

  _textField.delegate = self;
_textField.keyboardType = UIKeyboardTypeDecimalPad;
[_textField setReturnKeyType:UIReturnKeyDone];

and now add the buttons to keyboard:

    UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
    [keyboardDoneButtonView sizeToFit];

    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", nil)
                                                                   style:UIBarButtonItemStyleDone target:self

    UIBarButtonItem* seperatorItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

    UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel", nil)
                                                                     style:UIBarButtonItemStylePlain target:self

    [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:cancelButton,seperatorItem, doneButton, nil]];
    _textField.inputAccessoryView = keyboardDoneButtonView;

and to hide the keyboard:

    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    [textField resignFirstResponder];
    return YES;

and your Done action method is:

    [_textField resignFirstResponder];

and Cancel action method is:

[_textField resignFirstResponder];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download