Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make text field "become first responder" in UIAlertView

I have two text fields in a UIAlertView. I want the second one to become the first responder so that the user doesn't have to tap on it. After I show the alert view, I have this code:

[textField becomeFirstResponder];

The only problem is that it does not work. The first text field has the typer in it. Any ideas? Thanks for your help.

like image 858
Jack Humphries Avatar asked Dec 27 '11 15:12

Jack Humphries


1 Answers

I had this same problem with trying to set focus to the password field in the UIAlertView (I was pre-populating the username). I had to use the delegate method:

- (void)didPresentAlertView:(UIAlertView *)alertView {
    UITextField *textField = [alertView textFieldAtIndex:0];
    [textField becomeFirstResponder];
}

If I tried it on willPresentAlertView or when I create the alertView, it didn't work, only on didPresentAlertView. I hope this helps.

like image 154
Travis M. Avatar answered Oct 06 '22 13:10

Travis M.