iOS multiple text fields on a UIAlertview In IOS 7

15,934

Solution 1

The only solution i found using UIAlertView with more than one text field in iOS7 is for login only.

use this line to initialize your alertView

[alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];

and this to grab the users input:

user = [alert textFieldAtIndex:0].text;
pw = [alert textFieldAtIndex:1].text

For other purposes than login view the other threads like this on: UIAlertView addSubview in iOS7

Solution 2

You can change accessoryView to any own customContentView in a standard alert view in iOS7

[alertView setValue:customContentView forKey:@"accessoryView"];

Note that you must call this before [alertView show].

Simplest illustrating example:

UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"TEST" message:@"subview" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
v.backgroundColor = [UIColor yellowColor];
[av setValue:v forKey:@"accessoryView"];
[av show];

enter image description here

Solution 3

If you want to add just two textfields to your UIAlertView, you can use UIAlertViewStyleLoginAndPasswordInput and modify the textfields as follows:

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Some Title" message:@"Some Message." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:@"No, thanks", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alert textFieldAtIndex:1].secureTextEntry = NO; //Will disable secure text entry for second textfield.
[alert textFieldAtIndex:0].placeholder = @"First Placeholder"; //Will replace "Username"
[alert textFieldAtIndex:1].placeholder = @"Second Placeholder"; //Will replace "Password"
[alert show];

Afterwards, in the UIAlertView delegate, you can simply get the text using:

text1 = [alert textFieldAtIndex:0].text;
text2 = [alert textFieldAtIndex:1].text;
Share:
15,934
Michael Choi
Author by

Michael Choi

Updated on July 26, 2022

Comments

  • Michael Choi
    Michael Choi almost 2 years

    So the new iOS 7 has come out and I'm trying to add multiple textFields and labels to the UIAlertviews. I need three. I've been trying to add them as subviews and that doesn't work anymore. I have also tried to add multiple lines with the UIAlertViewStylePlainTextInput but it only seems to return one text field.

    I need to add in labels to show them what to enter as well. Is there a way to accomplish this task with the new iOS 7?

  • Michael Choi
    Michael Choi over 10 years
    Yeuh thanks for the answer I ended up making a custom subview that looked like a uiAlertView.
  • Dan Rosenstark
    Dan Rosenstark about 10 years
    Thanks, this answer is great and also points to how utterly useless UIAlertView is... the message from Apple is clear: go and write code. It's the most productive thing you can do.
  • App Dev Guy
    App Dev Guy over 9 years
    Thanks for adding the details about the secureEntry and placeholder text. Much nicer answer because of this :-)
  • Milan Gupta
    Milan Gupta over 6 years
    this is not working in ios 11. Is there any idea that someone could suggest it.