can I put multiple text input boxes on the alert box in iOS?
-
Yes, you can. Why do you need this? There's a set layout style for login prompts if that's what you want. – thegrinner Mar 14 '13 at 16:20
-
1`UIAlertView` already supports 1 or 2 text fields. Please read the docs for `UIAlertView`. – rmaddy Mar 14 '13 at 16:49
3 Answers
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.

- 75,956
- 16
- 112
- 147
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.

- 12,947
- 4
- 35
- 62
-
3What 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
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/

- 2,071
- 1
- 25
- 24