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

I have two text fields in a UIAlertView. I want the second to be the first responder, so the user does not need to click on it. After I show the warning, I have this code:

[textField becomeFirstResponder]; 

The only problem is that it does not work. There is a typer character in the first text box. Any ideas? Thank you for your help.

+6
source share
3 answers

I had the same problem trying to set focus on the password field in UIAlertView (I prefilled the username). I had to use the delegate method:

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

If I tried this on willPresentAlertView or when I create an alertView, this did not work, only on didPresentAlertView. Hope this helps.

+12
source

You need to create a warning and add a UITextField to it. You can then designate a new text field as your first responder, and this will bring up the keyboard with your warning.

Here is a story that might be what you are looking for.

Warning view with tooltip

0
source

The marked answer is correct, I just add a little more clarification related to my comment above, here is the code; make sure the field exists before using it and looks at index 0:

  if ([alertView textFieldAtIndex:0]) { UITextField *textfield = [alertView textFieldAtIndex:0]; [textfield becomeFirstResponder]; } 
0
source

Source: https://habr.com/ru/post/904543/


All Articles