iosuipopovercontrolleriphone-keypad

KeyBoard gets locked after popover dismissal


There are multiple textfields in a viewcontroller in which some of them are customised (one tapping those textfield will launch a popover controller, from that user can select the option which will get displayed in tapped textfield).

I have a tap gesture on the view controller for dismissing the keyboard (if it's on the screen).

Keypad gets locked(if it's visible) when I open the popover controller on taping the customised textfield. The keyboard is not getting dismissed even if I tap on the parent view or else on the dismiss button in the keypad.

I have tried this 2 snippets to hide the keyboard, but it's not working

[self.scrollView endEditing:YES];
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];

Solution

  • You can use textfields delegate to prevent it from presenting a keyboard and instead present popover yourself by implementing this textFieldShouldBeginEditing method

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
        if(textField == myCustomTextField) {
            [self openCustomPopover];
            return NO;
        }
        return YES;
    }
    

    more on its delegate methods here https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/index.html#//apple_ref/occ/intfm/UITextFieldDelegate/textFieldShouldBeginEditing: