@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];
}
在显示 UIAlertView
之后,键盘也会显示,并且 UITextfield
是焦点(它只适用于 iOS7 之前的版本,但在 iOS8 中不起作用)。我试过 [textfield becomeFirstResponder]
,但没有用。我创建了新项目并使用上面的 cove,它有效。
我的问题:键盘未以 UIAlertViewStylePlainTextInput
样式显示。
不知道为什么?有人可以帮助我吗? 谢谢。
最佳答案
这是对 Chen Lim 的 hack/fix 的轻微改进,将放置在 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];
}
}
(您必须实现 isIOS8 方法、属性或 iVar。)
关于ios - UIAlertView 文本字段在 iOS 8 中不显示键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25563108/