Adding TextField to UIAlertView

9 Solutions Collect From Internet About “Adding TextField to UIAlertView”

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.

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.

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.

Project on gitgub

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
}

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];

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.

As of iOS 8 UIAlertView has been deprecated in favor of UIAlertController, which adds support for adding UITextFields using the method:

- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;

See this answer for an example.

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");
        }

    }
}

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);
}

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.