shajem
shajem

Reputation: 2121

Adding TextField to UIAlertView

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 ?

Upvotes: 21

Views: 38867

Answers (9)

Ankit Srivastava
Ankit Srivastava

Reputation: 12405

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.

Upvotes: 0

Vijay Rathod
Vijay Rathod

Reputation: 197

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

Upvotes: 1

Ric Santos
Ric Santos

Reputation: 16437

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.

Upvotes: 4

tmr
tmr

Reputation: 1530

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

    }
}

Upvotes: 0

Brandon Tennant
Brandon Tennant

Reputation: 186

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.

Upvotes: 14

Shmidt
Shmidt

Reputation: 16664

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.

Upvotes: 69

Andy Friese
Andy Friese

Reputation: 6469

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
}

Upvotes: 6

Abdullah Md. Zubair
Abdullah Md. Zubair

Reputation: 3324

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.

Upvotes: 3

fannheyward
fannheyward

Reputation: 19277

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

Upvotes: 4

Related Questions