UIAlertView with Two TextFields and Two Buttons

17,955

Solution 1

y==400 is about the bottom of a phone screen. I imagine then, the UIAlertView that never expected to either have subviews added or it's frame set, reset's it's frame to be centered. Can you comment out that very last line and see what happens?

Solution 2

Starting from iOS 5 is possible to use the property alertViewStyle of UIAlertView to get different type of alert views, also with text fields.

Possible values:

  • UIAlertViewStyleDefault
  • UIAlertViewStyleSecureTextInput
  • UIAlertViewStylePlainTextInput
  • UIAlertViewStyleLoginAndPasswordInput

You can use the UIAlertViewStyleLoginAndPasswordInput to obtain an UIAlertView with two text fields and then you can customize some properties of the textFields:

 UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Your_Title" message:@"Your_message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[av setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];

// Alert style customization 
[[av textFieldAtIndex:1] setSecureTextEntry:NO];
[[av textFieldAtIndex:0] setPlaceholder:@"First_Placeholder"];
[[av textFieldAtIndex:1] setPlaceholder:@"Second_Placeholder"];
[av show];

You can access the values of the text fields on the alertView:clickedButtonAtIndex: of UIAlertViewDelegate.

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
     NSLog(@"1 %@", [alertView textFieldAtIndex:0].text);
     NSLog(@"2 %@", [alertView textFieldAtIndex:1].text);
}

Solution 3

Just move the show statement below where you set the alert's frame

Solution 4

I use DDAlertPrompt in my applications. It's easy and a drop-in class.

Solution 5

I use following for the same purpose

-(void)alertView {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@""
                                                 message:@"Enter User & Password"
                                                delegate:self
                                       cancelButtonTitle:@"Cancel"
                                       otherButtonTitles:@"Login", nil];


alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
UITextField * alertTextField1 = [alert textFieldAtIndex:0];
alertTextField1.keyboardType = UIKeyboardTypeDefault;
alertTextField1.placeholder = @"User";

UITextField * alertTextField2 = [alert textFieldAtIndex:1];
alertTextField2.keyboardType = UIKeyboardTypeDefault;
alertTextField2.placeholder = @"Password";


[alert show];

}

If you do not use secure character in second text field add

alertTextField2.secureTextEntry=NO;
Share:
17,955
Akshay
Author by

Akshay

This place is available for rent.

Updated on July 26, 2022

Comments

  • Akshay
    Akshay almost 2 years

    Problem - I want a UIAlertView which contains a Title, two textfields with placeholders and two buttons. What I have done till now - I have used this code, I am getting the exact UIAlertView with all these I have mentioned but when the view is getting loaded is seems to appear in the bottom first and then comes into the middle of the view.

        -(void)showalert{
        disclaimerAgreedAlertView = [[UIAlertView alloc] initWithTitle:@"   " 
                                                               message:@"    " 
                                                              delegate:self 
                                                     cancelButtonTitle:@"Cancel" 
                                                     otherButtonTitles:@"Login", nil];
    
        userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 15.0, 245.0, 25.0)];
        userNameTextField.delegate=self;
        [userNameTextField setBackgroundColor:[UIColor whiteColor]];
        [userNameTextField setKeyboardType:UIKeyboardTypeDefault];
        userNameTextField.placeholder=@"Username";
        userNameTextField.secureTextEntry=NO;
        [disclaimerAgreedAlertView addSubview:userNameTextField];
    
    
        passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
        passwordTextField.delegate=self;
        [passwordTextField setBackgroundColor:[UIColor whiteColor]];
        [passwordTextField setKeyboardType:UIKeyboardTypeDefault];
        passwordTextField.placeholder=@"Password";
        passwordTextField.secureTextEntry=YES;
        [disclaimerAgreedAlertView addSubview:passwordTextField];
    
        disclaimerAgreedAlertView.tag=99;
    
        [disclaimerAgreedAlertView show];
        disclaimerAgreedAlertView.frame= CGRectMake(5, 400, 320, 160);
    
    }
    

    Then I tried some of the non documented api's which are giving me the result I want but I am afraid that it may cause rejection of my app.

    So, any solutions to this?

  • Akshay
    Akshay about 12 years
    I just missed it or I was doing it wrong since starting. Thanks for the help. After commenting the last line it seems to be working fine. Thanks :)
  • Nikita Vlasenko
    Nikita Vlasenko over 8 years
    The link gives me "404"
  • George Asda
    George Asda about 8 years
    the link is broken
  • mstottrop
    mstottrop about 8 years
    should be reachable now :)