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];
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
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With