0

can I put multiple text input boxes on the alert box in iOS?

Hassan Zaheer
  • 1,361
  • 2
  • 20
  • 34

3 Answers3

2

The best solution is to write or use a custom alert view that has the text fields that you need. If you only need 1 text field (or username/password) you can still use UIAlertView by setting the alertViewStyle property.

There are custom alert views already written, such as this one.

progrmr
  • 75,956
  • 16
  • 112
  • 147
1

Yes, that's possible. Just add UITextField as subviews to the UIAlertView. Set the tag for each UITextField, in order to retrieve the entered text later.

    UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];

    UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)];
    textField1.tag=0;
    textField1.borderStyle=UITextBorderStyleRoundedRect;
    textField1.delegate=self;

    [alert addSubview:textField1];

    UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 85.0, 260.0, 25.0)]; 
    textField2.tag=1;
    textField2.delegate=self;
    textField2.borderStyle=UITextBorderStyleRoundedRect;

    [alert addSubview:textField2];

    [alert show];

PLEASE NOTE that this is bad practice as it relies on UIAlertView being a subclass of UIView. As pointed out by MusiGenesis below, it won't work from iOS 7 onwards.

Hope that helps.

FD_
  • 12,947
  • 4
  • 35
  • 62
  • 3
    What happens when iOS 7 (or whatever version) comes out and alert views look or act differently? The `UIAlertView` is not meant to have random subviews added to it. It may work now but that doesn't mean it's a good approach. – rmaddy Mar 14 '13 at 16:48
  • @rmaddy: very prophetic of you - it appears that in the iOS 7 beta 2, you can't add subviews to a `UIAlertView` at all. – MusiGenesis Jun 26 '13 at 17:30
0

if you want 2 text boxes, you can use login style, but set second item secureTextEntry to NO, refer to http://forums.macrumors.com/threads/uialertviews-with-textfields.1355954/

Yao Li
  • 2,071
  • 1
  • 25
  • 24