Unable to add UITextField to UIAlertView on iOS7...works in iOS 6
The code below works on iOS6 (and before) but the UITextField does not display in iOS7...any ideas on how to get a UITextField to display in an UIAlterView in iOS7?
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:@"Enter ESC Score"];
[dialog setMessage:@" "];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
dialog.tag = 5;
nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setKeyboardType:UIKeyboardTypeNumberPad];
[nameField becomeFirstResponder];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 0.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];
[nameField release];
Code run for iOS6 displays this:
same code in iOS7 displays this (notice how UITextField is missing and there is no keyboard):
You can't easily alter the view hierarchy of a UIAlertView in iOS 7. (Nor should you; the documentation specifically tells you not to.) Head over to the developer forums to see a long discussion about it.
One alternative in your case is to set alert.alertViewStyle = UIAlertViewStylePlainTextInput;
This will add a text field for you. You can access it in the UIAlertView delegate callback by using UITextField *textField = [alertView textFieldAtIndex:0];
.
@Aaron Brager had the right solution. Additionally I added a line after his suggestion to default a Numeric Keypad.
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:@"Enter ESC Score"];
[dialog setMessage:@" "];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
dialog.tag = 5;
dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
[dialog textFieldAtIndex:0].keyboardType = UIKeyboardTypeNumberPad;
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 0.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Credit Card Number"
message:@"Please enter your credit card number:"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Ok", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
/* Display a numerical keypad for this text field */
UITextField *textField = [alertView textFieldAtIndex:0];
textField.keyboardType = UIKeyboardTypeNumberPad;
[alertView show];
Just working as charm
Two UITextField in UIAlertView for all version of iOS
-(IBAction) showAlertView {
UIAlertView *alert;
UITextField *callForwardNumber;
UItextField *callForwardCondition;
alert = [[UIAlertView alloc] initWithTitle:@"Enter Phone Number & Rule"
message:@""
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Save", nil];
//alert.transform = CGAffineTransformMakeTranslation(0, 110);
callForwardNumber = [[UITextField alloc] init];
callForwardNumber.keyboardType = UIKeyboardTypeNumberPad;
callForwardNumber.text = [R.prefs objectForKey:@"fwd_number"];
callForwardNumber.borderStyle = UITextBorderStyleRoundedRect;
callForwardNumber.delegate = self;
callForwardNumber.tag = 1;
callForwardCondition = [[UITextField alloc] init];
callForwardCondition.text = callCondition;
callForwardCondition.borderStyle = UITextBorderStyleRoundedRect;
callForwardCondition.delegate = self;
callForwardCondition.tag = 2;
[callForwardCondition setKeyboardType:UIKeyboardTypeNumberPad];
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
UIView* customAccessory =
[[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 55)];
callForwardNumber.frame = CGRectMake(0, 0, 245.0, 25.0);
callForwardCondition.frame = CGRectMake(0, 30.0, 245.0, 25.0);
[customAccessory addSubview:callForwardNumber];
[customAccessory addSubview:callForwardCondition];
[alert setValue:customAccessory forKey:@"accessoryView"];
[alert show];
} else {
alert.message = @"\n\n\n";
[alert show];
callForwardNumber.frame = CGRectMake(20.0, 45.0, 245.0, 25.0);
callForwardCondition.frame = CGRectMake(20.0, 75.0, 245.0, 25.0);
[alert addSubview:callForwardNumber];
[alert addSubview:callForwardCondition];
}
}