Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextField on AlertView not showing on iOS 7 [closed]

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?

like image 442
user2546570 Avatar asked Dec 02 '22 18:12

user2546570


2 Answers

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.

like image 172
Matthias Bauch Avatar answered Dec 14 '22 23:12

Matthias Bauch


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
    }
}
like image 44
Sarfaraz Khan Avatar answered Dec 14 '22 23:12

Sarfaraz Khan