Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertView's textfield does not show keyboard in iOS8

Tags:

ios

@implementation
UIAlertView *query;
@end

- (void)viewWillAppear:(BOOL)animated {
query = [[UIAlertView alloc]
                      initWithTitle:NSLocalizedString(@"New", @"")
                      message:nil
                      delegate:self
                      cancelButtonTitle:NSLocalizedString(@"Cancel", nil)
                      otherButtonTitles:NSLocalizedString(@"OK", @""), nil];
query.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *textField = [_query textFieldAtIndex:0];
textField.placeholder = NSLocalizedString(@"Title", @"placeholder text where user enters name for new playlist");
textField.delegate = self;
[_query show];
}

After UIAlertView is shown, keyboard is shown too and UITextfield is focus (It just works on pre iOS7, but does not work in iOS8). I have tried [textfield becomeFirstResponder], but it does not worked. I create new project and use cove above, it works.

My problem: Keyboard does not shown with UIAlertViewStylePlainTextInput style.

I don't know why? Can anybody helps me? Thanks.

like image 224
nmh Avatar asked Aug 29 '14 06:08

nmh


4 Answers

Here's a slight improvement to Chen Lim's hack/fix, to be placed in the UIAlertViewDelegate:

- (void)didPresentAlertView:(UIAlertView *)alertView {
    if (isIOS8) {
        // hack alert: fix bug in iOS 8 that prevents text field from appearing
        UITextRange *textRange = [[alertView textFieldAtIndex:0] selectedTextRange];
        [[alertView textFieldAtIndex:0] selectAll:nil];
        [[alertView textFieldAtIndex:0] setSelectedTextRange:textRange];
    }
}

(You'll have to implement an isIOS8 method, property, or iVar.)

like image 127
cetcet Avatar answered Nov 07 '22 15:11

cetcet


This ugly hack worked for me, after calling show:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    // HACKHACK: This is to make the keyboard appear in iOS8
    UITextRange* textRange = self.textField.selectedTextRange;
    [self.textField selectAll:self];
    [UIMenuController sharedMenuController].menuVisible = NO;
    self.textField.selectedTextRange = textRange;
});

Ran into the same problem, and spent hours fiddling with UIAlertController, which is the "right" solution, but unfortunately has several issues elsewhere (e.g. it breaks when presenting from a popover), and won't work with iOS7.

This is definitely a bug in iOS8. I hope Apple fixes this soon. It unfortunately breaks even with older binaries compiled for iOS7.

like image 43
Chen Lim Avatar answered Nov 07 '22 15:11

Chen Lim


This helps me to fix all related issues:

[MyUITextView endEditing:YES];

Put it before showing UIAlertView.

like image 3
Dmitry Avatar answered Nov 07 '22 13:11

Dmitry


In my case this was happening because the UIViewController that was presenting the UIAlertView had implemented canBecomeFirstResponder with a YES result since I wanted to listen for 'shake' events.

Once I added the following implementation of the UIAlertViewDelegate method the keyboard was presented as expected:

- (void)willPresentAlertView:(UIAlertView *)alertView
{
    [self resignFirstResponder];
}

So this may not solve all of the problems with this so-called 'deprecated' class but it is something to check if your controller is enabling first-responder behaviour.

like image 2
Christopher King Avatar answered Nov 07 '22 14:11

Christopher King