UIAlertView Vs UIAlertController - no keyboard in iOS 8

邮差的信 提交于 2019-11-29 18:30:17

问题


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

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.

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 keyboard is displayed.

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


回答1:


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

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

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




回答2:


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



来源:https://stackoverflow.com/questions/26074475/uialertview-vs-uialertcontroller-no-keyboard-in-ios-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!