Change the keyboard type of UITextField in UIAlertView

ios, objective-c, uialertview

Solution

If You want to present the Input Style, First of all implement the UIAlertViewDelegate in your class.

Secondly when you presenting the alertView set the delegate to self.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"a" message:@"b" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"aaa", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;  
[alert show];'

If you want to change the keyboard type for particular field then do like this

e.g for 1st field, it will make the keyboard numeritc.

[[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[[alert textFieldAtIndex:0] becomeFirstResponder];

UPDATE for iOS 8.0 Swift

From ios 8.0 onward `UIAlertView` is deprecated so you might need to use `UIAlertController`

 var alert = UIAlertController(title: "Title", message: "Your msg", 
                preferredStyle: UIAlertControllerStyle.Alert)

        alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, 
              handler:yourHandler))

        alert.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
            textField.placeholder = "Password"
            textField.secureTextEntry = true  // setting the secured text for using password
            textField.keyboardType = UIKeyboardType.Default
        })

Problem

I created a UIAlertView with its `alertViewStyle` set to `UIAlertViewStylePlainTextInput`. I want to change the keyboard type, but not by adding a subview. My Code: ``` UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"a" message:@"b" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"aaa", nil]; alert.alertViewStyle = UIAlertViewStylePlainTextInput; [alert show]; ```

Original source