Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertView Vs UIAlertController - no keyboard in iOS 8

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?

enter image description here

like image 648
Sam B Avatar asked Sep 27 '14 12:09

Sam B


2 Answers

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

like image 132
Bhumit Mehta Avatar answered Oct 08 '22 12:10

Bhumit Mehta


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

like image 31
voghDev Avatar answered Oct 08 '22 12:10

voghDev