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,
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.
source share