UIAlertView与UIAlertController相同 - iOS 8中没有键盘

Sam*_*m B 10 xcode objective-c ios ios-simulator

我有一个针对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];
Run Code Online (Sandbox Code Playgroud)

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

    }
Run Code Online (Sandbox Code Playgroud)

再次,如果我尝试相同的操作,则不显示键盘.

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

在此输入图像描述

Bhu*_*hta 19

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

按"Command"+"k",您应该能够看到键盘.

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

希望这可以帮助

  • 它会起作用,但它会被折旧,所以苹果可能会在即将发布的iOS版本中停止这种情况,所以不要使用已删除的方法并保持最新状态这是一个好习惯,但现在它应该没有问题 (3认同)