37

I have this code to prompt the UIAlertView, with UITextfield:

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"New List Item", @"new_list_dialog")
                                                      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:myTextField];
[myAlertView show];
[myAlertView release];

But I would like to add a get the textfield value, after the user click "OK", and after the user click , I want to call a method, how can I assign that to the myAlertView? Thank you.

Jayesh Thanki
  • 2,037
  • 2
  • 23
  • 32
DNB5brims
  • 29,344
  • 50
  • 131
  • 195
  • Just bear in mind this is not officially supported by the SDK and is liable to break any time Apple changes the UIAlertView implementation. And this isn't as unlikely as you think, all those hacked decimal keys on the numeric keypad broke on iOS 4, so this stuff does happen. – Mike Weller Sep 03 '10 at 09:00
  • but is there any "official" way to do so? – DNB5brims Sep 03 '10 at 09:10
  • @DNB5brims I see that you are an active SO user and this is a highly viewed question (45k views in a year). Could you consider changing the accepted answer to the *official* way of doing so as answered by "Hamed Rajabi" below. UIAlertView now has a built in property of `UIAlertViewStylePlainTextInput` – Albert Renshaw Jan 16 '17 at 03:31
  • Just used the accepted solution painstakingly and with error, only to come back and see the second answer in this thread is an official documented way of doing this. Then came to this comment section urge you to update the selected answer to the official solution and save others lots of wasted time I just experienced, and I see that I already asked you to do so over 3 years ago. Shame that still hasn't changed. – Albert Renshaw Jun 06 '20 at 01:13

6 Answers6

202

If you want to add a TextField to an UIAlertView, you can use this property (alertViewStyle) for UIAlertView:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
                                                message:@"Message"
                                               delegate:self
                                      cancelButtonTitle:@"Done"
                                      otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
[alert release];

and in .h file of it add UIAlertViewDelegate as a protocol and implement the alertView:clickedButtonAtIndex delegate method in the .m file:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%@", [alertView textFieldAtIndex:0].text);
}

I hope, it works for you!

Note: "Available in iOS 5.0 and later"

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
Hamed Rajabi Varamini
  • 3,439
  • 3
  • 24
  • 38
  • Are you sure that is good for the AppStore submissions?..i remember that textFieldAtIndex is a private method..time ago apple rejects me an app for this method...or maybe in iOS 5 is different(?) – Mat Oct 22 '11 at 13:26
  • 1
    I saw [http://developer.apple.com/library/IOS/#documentation/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html](http://developer.apple.com/library/IOS/#documentation/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html). On this page wrote "Available in iOS 5.0 and later". – Hamed Rajabi Varamini Oct 22 '11 at 14:52
  • UIAlertView is deprecated since iOS 9.0 – Alex Cio Jan 01 '17 at 13:40
30

Declare the text field as global.And in the method of alertView clicked - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex just take the value of the textfield and do the operations you want with it.....

Heres the revised code

UITextField *myTextField;
...
{

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"New List Item", @"new_list_dialog")
                                                      message:@"this gets covered" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myAlertView addSubview:myTextField];
[myAlertView show];
[myAlertView release];
}
....
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"string entered=%@",myTextField.text);
}

For iOS 5 and later You can use alertViewStyle property of UIAlertView.

Please refer Hamed's Answer for the same

Suresh Varma
  • 9,750
  • 1
  • 60
  • 91
  • 1
    this is a very lazy answer. perhaps you should fully ANSWER the question – james Feb 03 '11 at 19:46
  • default property is there to show alert with textField then why create textField programatically.. – Hardik Thakkar Feb 21 '16 at 08:39
  • @Gamechanger This is a 5 year old thread and also it is mentioned in the answer already that **For iOS 5 and later You can use alertViewStyle property of UIAlertView** – Suresh Varma Feb 21 '16 at 08:58
21

As of iOS 8 UIAlertView has been deprecated in favor of UIAlertController, which adds support for adding UITextFields.

Swift

let alert = UIAlertController(title: "Title",
                              message: nil,
                              preferredStyle: .alert)
alert.addTextField { (textField) in
    // optionally configure the text field
    textField.keyboardType = .alphabet
}

let okAction = UIAlertAction(title: "OK", style: .default) { [unowned alert] (action) in
    if let textField = alert.textFields?.first {
        print(textField.text ?? "Nothing entered")
    }
}
alert.addAction(okAction)

self.present(alert, animated: true, completion: nil)

Objective-C

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title"
                                                               message:nil
                                                        preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    // optionally configure the text field
    textField.keyboardType = UIKeyboardTypeAlphabet;
}];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK"
                                                   style:UIAlertActionStyleDefault
                                                 handler:^(UIAlertAction *action) {
                                                         UITextField *textField = [alert.textFields firstObject];
                                                 }];
[alert addAction:okAction];

[self presentViewController:alert animated:YES completion:nil];
Ric Santos
  • 15,419
  • 6
  • 50
  • 75
  • 1
    But in Objective-C code, there is a retain cycle: alert owns action, action owns handler block, which owns alert... – Radek Wilczak Jan 02 '19 at 15:20
  • @RadekWilczak Nice catch! Declare `__weak UITextField *weakTextField = [alertController textFields][0];` outside the action, and then inside the action block use `weakTextField.text` – Albert Renshaw Jun 06 '20 at 02:40
2
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Login" message:nil delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
alert.alertViewStyle=UIAlertViewStylePlainTextInput;
[alert show];
Prahalad Gaggar
  • 11,389
  • 16
  • 53
  • 71
0

You need a global variable for the UITextfield, you want to retrieve value in your AlertView Delegate method.I have created a post in my blog on the topic "How to add UITextField to UIAlertView from XIB". You can take a look at the following link.

http://creiapp.blogspot.com/2011/08/how-to-add-uitextfield-to-uialertview.html

Cullen SUN
  • 3,517
  • 3
  • 32
  • 33
0
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hey Welcome"

message:@"MSG" 

delegate:self

cancelButtonTitle:@"Ok Ji" 

otherButtonTitles:nil];

UITextField textField = [[UITextField alloc] initWithFrame:CGRectMake(15.0, 70.0, 200.0, 25.0)];

[textField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:textField];

[alert show];
[alert release];