xcode UIAlertView 与 UIAlertController - iOS 8 中没有键盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26074475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:43:11  来源:igfitidea点击:

UIAlertView Vs UIAlertController - no keyboard in iOS 8

iosobjective-cxcodeios-simulator

提问by Sam B

I have a old Xcode project that is targeted for iOS 6 and higher. I recently opened it up in Xcode 6 and ran in iPhone 6 simulator for iOS 8. When I tried this action

我有一个针对 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];

I get a pop-up window but when I click on the text field, no keyboard comes up. I googled and read that I need to use UIAlertController instead. Since I need to support iOS 6, 7 versions as well so I changed my code like this.

我得到一个弹出窗口,但是当我单击文本字段时,没有键盘出现。我用谷歌搜索并读到我需要使用 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];

    }

Again if I try the same action NO keyboardis displayed.

同样,如果我尝试相同的操作,则不会显示键盘

Is this a bug in Xcode 6 simulator or I am doing something wrong?

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

enter image description here

在此处输入图片说明

回答by Bhumit Mehta

I Tested this, if iOS8 detects a hardware keyboard , than it does not open the keypad. As you might be testing in simulator , so it is not Showing any keypad

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

Press "Command" + "k" and you should be able to see the keypad.

按“Command”+“k”,您应该能够看到键盘。

When testing on a device you will not face this issue , unless user has connected his device with a hardware Bluetooth keypad , so this is not something you should worry about

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

Hope this helps

希望这可以帮助

回答by voghDev

Another way of seeing the keyboard is setting a keyboard type. This could be an example code:

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

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];

Then you will see simulator's keyboard when you show the AlertController

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