UIAlertView与UIAlertController - iOS 8中无键盘

10

我有一个早期的Xcode项目,针对iOS 6及更高版本进行了定位。最近我在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。由于我还需要支持iOS6、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模拟器中的一个bug还是我做错了什么?

在此输入图片描述


1
尝试按下“Command” +“K”,也许iOS正在检测到硬件键盘,因此它没有显示键盘。 - Bhumit Mehta
在模拟器菜单选项中,选择“硬件”-->“键盘”-->“切换软键盘”。 - yazh
2个回答

18

我测试过了,如果iOS8检测到有硬件键盘,它就不会打开虚拟键盘。可能你是在模拟器上测试,所以没有显示任何键盘

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

当你在设备上测试时,除非用户连接了他的设备与硬件蓝牙键盘,否则你不会遇到这个问题,所以这不是你需要担心的事情

希望这可以帮助


谢谢你查看这个。我会标记你的答案。还有一个问题,如果我只在iOS 8设备上使用UIAlertView,那么代码不会工作吗?我在模拟器中尝试过,它确实可以工作。 - Sam B
3
这个方法虽然可以运作,但已经被废弃了,苹果公司可能会在未来的iOS版本中停止支持它,因此最好不要使用已废弃的方法并保持更新。但目前暂时使用应该没有问题。 - Bhumit Mehta

1

另一种看待键盘的方式是设置键盘类型。以下是一个示例代码:

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时,将会看到模拟器的键盘。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接