I’m trying to set the initial focus inside a UIAlertView to the password field, but all I’ve tried so far didn’t work.
I was hoping that this code works but there’s a mistake that I can’t figure out:
- (void)showLoginFormWithUsername:(NSString*)username andPassword:(NSString*)password {
UIAlertView *loginView = [[UIAlertView alloc] initWithTitle:@"Login"
message:@"Please enter your login data."
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Login", nil];
loginView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
// get TextFields
UITextField *usernameField = [loginView textFieldAtIndex:0];
UITextField *passwordField = [loginView textFieldAtIndex:1];
// set text
if (username)
usernameField.text = username;
if (password)
passwordField.text = password;
// set focus
if (usernameField.hasText && !passwordField.hasText)
[passwordField becomeFirstResponder];
[loginView show];
}
What am I doing wrong?
You aren’t waiting for the alert view to show before you are trying to make it be a responder. Instead, do this in the
didPresentAlertView:method.