29

The updated UIAlertView now has a style which allows a Text input field in UIAlertView i.e.

alert.alertViewStyle = UIAlertViewStylePlainTextInput;

This works well but I wanted to init the input text with a defualt text like "sample".

I see that folks in the past have used undocumented api like (which works great)

[alert addTextFieldWithValue:@"sample text" label:@"Text Field"];

but since this is still not an official Apple public API I can't use it.

Any other way to handle this? I did try to init in willPresentAlertView but text field seem to be read-only.

Thanks

Mutix
  • 4,196
  • 1
  • 27
  • 39
timeview
  • 291
  • 1
  • 3
  • 5

5 Answers5

57

The UIALertView has a textFieldAtIndex: method that returns the UITextField object you want.

For a UIAlertViewStylePlainTextInput, the index of the textfield is 0.

You can then set the placeholder (or text) property of the textfield:

UIAlertView *alert = ....
UITextField *textField = [alert textFieldAtIndex:0];
textField.placeholder = @"your text";

UIAlertView Class Reference

Mutix
  • 4,196
  • 1
  • 27
  • 39
  • This doesn't seem to work any more. textFieldAtIndex generates this error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'textFieldIndex (0) is outside of the bounds of the array of text fields'. – Symmetric Nov 06 '13 at 19:38
  • worked for me, but how come !!!! how the effect reflects to the alert !! can't understand ! – OXXY Jan 19 '14 at 10:15
  • 1
    @Symmetric, make sure you set [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput]; before trying to get the UITextField – bubastis Apr 10 '14 at 14:15
9

Easy way to do

 UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"your title" message:@"your message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    alerView.alertViewStyle = UIAlertViewStylePlainTextInput;
    [[alerView textFieldAtIndex:0] setPlaceholder:@"placeholder text..."];
    [alerView show];
Dipu Rajak
  • 693
  • 5
  • 15
8

Not tested, but I assume this would work:

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:...];
UITextField* textField = [alert textFieldAtIndex:0];
textField.text = @"sample";
[alert show];
Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152
6

For setting default value in uiAlertView this thing will work.

UIAlertView *alert = ....
UITextField *textField = [alert textFieldAtIndex:0];
[textField setText:@"My default text"];
[alert show];
Salman Iftikhar
  • 311
  • 2
  • 9
0
- (IBAction)showMessage:(id)sender {
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Add New Category"
                                                      message:nil
                                                     delegate:self 
                                            cancelButtonTitle:@"Add"
                                            otherButtonTitles:@"Cancel", nil];
    [message setAlertViewStyle:UIAlertViewStylePlainTextInput];
    [message show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Add"])
    {
        UITextField *username = [alertView textFieldAtIndex:0];
        NSLog(@"Category Name: %@", username.text);
    }
}
user948749
  • 45
  • 1
  • 5