TextField in AlertView not showing on iOS 7

Prior to iOS7, I could add a UITextField (text input field) to my UIAlertView 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 updating to iOS7, it stops working - my text fields are no longer displayed. What is the recommended way to update my code?

+4
source share
4 answers

You want to use the β€œnew” (iOS 5) UIAlertView methods that UITextField provides you with. 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 has never been allowed . The hierarchy of views has always been confidential. Apple has begun to apply this restriction.

If you want to customize UIAlertView, you need to write your own. Subclass UIView and make it look like UIAlertView.

+7
source
 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 } } 
+7
source

As suggested by Matthias Bauch, you do not need to add a text box to the alert view, use the UIAlertView alertViewStyle property alertViewStyle . It takes the values ​​defined in the UIAlertViewStyle enumeration.

 typedef NS_ENUM(NSInteger, UIAlertViewStyle) { UIAlertViewStyleDefault = 0, UIAlertViewStyleSecureTextInput, // Secure text input UIAlertViewStylePlainTextInput, // Plain text input UIAlertViewStyleLoginAndPasswordInput // Two text fields, one for username and other for password }; 

In your case, to use this, follow this code.

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please enter password" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Continue", nil]; [alert setAlertViewStyle:UIAlertViewStyleSecureTextInput]; [alert show]; 

To verify the input, let's say that the password entered must be at least 6 characters, implement this delegate method,

 - (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView { NSString *inputText = [[alertView textFieldAtIndex:0] text]; if( [inputText length] >= 6 ) { return YES; } else { return NO; } } 

To enter user input

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; if([title isEqualToString:@"Login"]) { UITextField *password = [alertView textFieldAtIndex:0]; NSLog(@"Password: %@", password.text); } } 

UIAlertView has a personal hierarchy of views, and it is recommended to use it as is without changes. Therefore, addSubview: to the type of warning will not affect its presentation.

From Apple docs

The UIAlertView class is intended to be used as is and does not support subclasses. The presentation hierarchy for this class is private and should not be changed.

+3
source

UIAlertView with UITextField ..

 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@" " delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; CGRect frame = CGRectMake(14, 45, 255, 23); UITextField *textField = [[UITextField alloc] initWithFrame:frame]; textField.placeholder = @"Name"; textField.backgroundColor = [UIColor whiteColor]; textField.autocorrectionType = UITextAutocorrectionTypeDefault; textField.keyboardType = UIKeyboardTypeAlphabet; textField.returnKeyType = UIReturnKeyDone; textField.clearButtonMode = UITextFieldViewModeWhileEditing; // has 'x' button to the right [alertView addSubview:textField]; [alertView show]; 

Courtesy of http://kshitizghimire.com.np/uitextfield-in-uialertview/

+1
source

Source: https://habr.com/ru/post/1498155/


All Articles