ios - UIAlertView 文本字段在 iOS 8 中不显示键盘

标签 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];
}

在显示 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/

相关文章:

iphone - 如何将部署目标设置为 5.0.1

ios - iPad上,基于UIView的新类的initWithFrame没有被调用?

ios - 为 iOS 版 Google map 位置构建 URL

ios - iOS 应用程序的远程访问

ios - 根据值使用不同颜色的 Swift iOS LineChart

objective-c - 为 Sprite 生成随机 Y

ios - 使用 GCD 计算从后台队列加载数据的百分比

ios - 如何从 xib 文件更改 iPhone 和 iPad 中的键盘布局?

ios - “PFObject”不是 'Post' 的子类型 - Swift 错误

css - 如何修复 Mobile Safari 上奇怪的文本输入字段错误?