@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.
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.)
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.
This helps me to fix all related issues:
[MyUITextView endEditing:YES];
Put it before showing UIAlertView
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With