I need to add a TextField
to an UIAlertView
. I understand that apple discourage this approach. So is there any library that i could make use of to add a TextField
to a UIAlertView
look-alike frame ?

- 2,111
- 5
- 22
- 30
9 Answers
For iOS5:
UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Title" message:@"Please enter someth" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
[av textFieldAtIndex:0].delegate = self;
[av show];
Also, you 'll need to implement UITextFieldDelegate
, UIAlertViewDelegate
protocols.

- 16,436
- 18
- 88
- 136
Unfortunately the only official API for this is iOS 5 and up, it's a property called alertViewStyle
which can be set to the following parameters:
UIAlertViewStyleDefault
UIAlertViewStyleSecureTextInput
UIAlertViewStylePlainTextInput
UIAlertViewStyleLoginAndPasswordInput
UIAlertViewStylePlainTextInput
being the one you want.
Messing with the view hierarchy as described above is strongly discouraged by Apple.

- 26,840
- 19
- 119
- 186

- 186
- 4
-
I have to create this for iOS 4 and 5. So are you aware of any other library where i could use – shajem Apr 01 '12 at 08:01
I'm using BlockAlertsAndActionSheets instead of the Apple components for AlertViews and ActionSheets as I prefer the blocks-approach. Also contains a BlockTextPromptAlertView
in the source, which might be what you want. You can replace the images of that control to get the Apple-style back.
Tutorial which gets you started
Example:
- (IBAction)newFolder:(id)sender {
id selfDelegate = self;
UITextField *textField;
BlockTextPromptAlertView *alert = [BlockTextPromptAlertView promptWithTitle :@"New Folder"
message :@"Please enter the name of the new folder!"
textField :&textField];
[alert setCancelButtonWithTitle:@"Cancel" block:nil];
[alert addButtonWithTitle:@"Okay" block:^{
[selfDelegate createFolder:textField.text];
}];
[alert show];
}
- (void)createFolder:(NSString*)folderName {
// do stuff
}

- 6,349
- 3
- 20
- 17
-
Can you show me an example that uses `BlockTextPromptAlertView `. I am under the impression that developers are not allowed to add any textfields etc to UIAlertView. – shajem Apr 01 '12 at 07:15
-
You're right with your impression. That's why BlockAlertsAndActionSheets is **not** using UIAlertView at all, it's a completely independent implementation without any private API's being used. I don't have access to my sources right now, but can update my answer with an code sample in some hours. – Andy Friese Apr 01 '12 at 08:05
-
-
If you would like to change the way the control is animated, try searching for `animateWithDuration` in BlockAlertView.m and BlockTextPromptAlertView.m – Andy Friese Apr 02 '12 at 06:19
As of iOS 8 UIAlertView
has been deprecated in favor of UIAlertController
, which adds support for adding UITextField
s using the method:
- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;
See this answer for an example.

- 1
- 1

- 15,419
- 6
- 50
- 75
Try something like this:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Title"
message:@"\n\n"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Save", nil] autorelease];
CGRect rect = {12, 60, 260, 25};
UITextField *dirField = [[[UITextField alloc] initWithFrame:rect] autorelease];
dirField.backgroundColor = [UIColor whiteColor];
[dirField becomeFirstResponder];
[alert addSubview:dirField];
[alert show];

- 18,599
- 12
- 71
- 109
You can try:
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Your title here!" message:@"this gets covered" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myAlertView addSubview:testTextField];
[myAlertView show];
[myAlertView release];
Follow this link for detail.

- 3,312
- 2
- 30
- 39
-
Yes, your code works. But Apple discourages this approach, as its using private APIs. SO is there any other way i could solve this ? – shajem Apr 01 '12 at 07:17
-
@anonymous The OP refers to that sentence from the docs: The view hierarchy for this class is private and must not be modified. – Andy Friese Apr 01 '12 at 08:53
first of All Add UIAlertViewDelegate into ViewController.h File like
#import <UIKit/UIKit.h>
@interface UIViewController : UITableViewController<UIAlertViewDelegate>
@end
and than Add Below Code where you wants to alert Display,
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Message"
delegate:self
cancelButtonTitle:@"Done"
otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
and it's delegate method which returns what input of UItextField
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%@", [alertView textFieldAtIndex:0].text);
}

- 197
- 1
- 3
- 11
adding to answer from 'Shmidt', the code to capture text entered in UIAlertView is pasted below (thank you 'Wayne Hartman' Getting text from UIAlertView)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
self.userNumber = [alertView textFieldAtIndex:0].text;
if (self.userNumber) {
// user enetered value
NSLog(@"self.userNumber: %@",self.userNumber);
} else {
NSLog(@"null");
}
}
}
refer this... http://iosdevelopertips.com/undocumented/alert-with-textfields.html this is private api and if you use it for app in appstore it might get rejected but it is fine for enterprise development.

- 37,241
- 25
- 195
- 267

- 12,347
- 11
- 63
- 115
-
Thank you for your reply, but i am hoping to publish my app to appstore :S – shajem Apr 01 '12 at 07:16
-