Before iOS7 I could add a UITextField
(text input field) to my UIAlertView
by using this code.
UITextField *txtNewPassword = [[UITextField alloc] initWithFrame:secondTextFldRect];
txtNewPassword.delegate = self;
txtNewPassword.text = @"";
txtNewPassword.clearButtonMode = UITextFieldViewModeWhileEditing;
txtNewPassword.borderStyle = UITextBorderStyleRoundedRect;
txtNewPassword.autocapitalizationType = UITextAutocapitalizationTypeNone;
txtNewPassword.tag = kNewPasswordTxtFldTag;
[txtNewPassword setBackgroundColor:[UIColor whiteColor]];
[txtNewPassword setKeyboardAppearance:UIKeyboardAppearanceAlert];
[txtNewPassword setAutocorrectionType:UITextAutocorrectionTypeNo];
[txtNewPassword setPlaceholder:@"New password"];
[txtNewPassword setTextAlignment:UITextAlignmentLeft];
[txtNewPassword setSecureTextEntry:YES];
[alert addSubview:txtNewPassword];
[txtNewPassword release];
After the update to iOS7 it stopped working - my text fields are no longer showing up. What's the advised way of updating my code?
You want to use the "new" (iOS 5) methods of UIAlertView that provide you with a UITextField. alertViewStyle
and textFieldAtIndex:
Which reduces your code to this:
UIAlertView *alert = [[UIAlertView alloc] ...];
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
UITextField *txtNewPassword = [alert textFieldAtIndex:0];
txtNewPassword.delegate = self;
txtNewPassword.text = @"";
txtNewPassword.clearButtonMode = UITextFieldViewModeWhileEditing;
txtNewPassword.tag = kNewPasswordTxtFldTag;
[txtNewPassword setPlaceholder:@"New password"];
Your code does not work on iOS7 because adding subViews to UIAlertView was never allowed. The view hierarchy has always been private. Apple started to enforce this restriction.
If you want a customized UIAlertView you have to write your own. Subclass UIView and make it look like UIAlertView.
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
dialog.alertViewStyle=UIAlertViewStylePlainTextInput;
[dialog setTitle:@"Your Title"];
[dialog setMessage:@"your message"];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"Ok"];
UITextField *_UITextField = [dialog textFieldAtIndex:0];
_UITextField.placeholder = @"Placeholder";
_UITextField.keyboardType = UIKeyboardTypeEmailAddress;
[dialog show];
//uialertview delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==1)//OK button
{
//do ur stuff
}
}
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