UIAlertView Vs UIAlertController – iOS 8中没有键盘

我有一个旧的 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];

我得到一个弹出窗口,但当我点击文本字段,没有键盘出现.我googled和阅读,我需要使用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模拟器中的错误还是我做错了?

解决方法

我测试过,如果iOS8检测到硬件键盘,那么它不会打开键盘.你可能正在模拟器中测试,所以它不是显示任何键盘

按“Command”“k”,你应该可以看到键盘.

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

希望这可以帮助

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...