【问题标题】:UIAlertView Vs UIAlertController - no keyboard in iOS 8UIAlertView 与 UIAlertController - iOS 8 中没有键盘
【发布时间】:2014-11-22 08:17:10
【问题描述】:

我有一个针对 iOS 6 及更高版本的旧 Xcode 项目。我最近在 Xcode 6 中打开它并在 iOS 8 的 iPhone 6 模拟器中运行。当我尝试此操作时

UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Enter Folder Name"
                                                     message:@"Keep it short and sweet"
                                                    delegate:self
                                           cancelButtonTitle:@"Cancel"
                                           otherButtonTitles:@"OK", nil];

    dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
    dialog.tag = 400;
    [dialog show];

我得到一个弹出窗口,但是当我单击文本字段时,没有键盘出现。我用谷歌搜索并读到我需要使用 UIAlertController 代替。由于我还需要支持 iOS 6、7 版本,所以我像这样更改了我的代码。

if ([UIAlertController class])
    {
        // use UIAlertController
        UIAlertController *alert= [UIAlertController
                                      alertControllerWithTitle:@"Enter Folder Name"
                                      message:@"Keep it short and sweet"
                                      preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction * action){
                                                       //Do Some action here
                                                       UITextField *textField = alert.textFields[0];
                                                       NSLog(@"text was %@", textField.text);

                                                   }];
        UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction * action) {

                                                           NSLog(@"cancel btn");

                                                           [alert dismissViewControllerAnimated:YES completion:nil];

                                                       }];

        [alert addAction:ok];
        [alert addAction:cancel];

        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.placeholder = @"folder name";
            textField.keyboardType = UIKeyboardTypeDefault;
        }];

        [self presentViewController:alert animated:YES completion:nil];

    }
    else
    {
        // use UIAlertView
        UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Enter Folder Name"
                                                         message:@"Keep it short and sweet"
                                                        delegate:self
                                               cancelButtonTitle:@"Cancel"
                                               otherButtonTitles:@"OK", nil];

        dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
        dialog.tag = 400;
        [dialog show];

    }

如果我再次尝试相同的操作,没有键盘会显示。

这是 Xcode 6 模拟器中的错误还是我做错了什么?

【问题讨论】:

  • 尝试按 "Command" +"K" ,可能是 iOS 正在检测硬件键盘所以它没有显示键盘
  • 在模拟器菜单选项硬件--> 键盘--> 切换软件键盘

标签: ios objective-c xcode ios-simulator


【解决方案1】:

我对此进行了测试,如果 iOS8 检测到硬件键盘,则它不会打开键盘。由于您可能正在模拟器中进行测试,所以它没有显示任何键盘

按“Command”+“k”应该可以看到键盘了。

在设备上进行测试时,您不会遇到此问题,除非用户已将设备与硬件蓝牙键盘连接,因此您无需担心

希望对你有帮助

【讨论】:

  • 谢谢老兄,检查一下。我会标记你的答案。还有一个问题。如果我只对 iOS 8 设备使用 UIAlertView,那代码会不会工作?我在模拟器中尝试过,它确实有效
  • 它会起作用,但它会被贬值,所以苹果可能会在即将发布的 iOS 版本中停止这个,所以最好不要使用过时的方法并保持最新,但现在应该没问题
【解决方案2】:

查看键盘的另一种方式是设置键盘类型。这可能是一个示例代码:

UIAlertController* alertController = [UIAlertController
                                     alertControllerWithTitle:@"Test"
                                     message:@"Testing keyboard"
                                     preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler: ^(UITextField *tf)
 {
     tf.autocorrectionType = UITextAutocorrectionTypeYes;
     tf.keyboardType = UIKeyboardTypeDefault;
 }];
[self presentViewController:alertController animated:YES completion:nil];

然后你会在显示 AlertController 时看到模拟器的键盘

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多