Ios UIAlertView中的焦点密码文本字段

Ios UIAlertView中的焦点密码文本字段,ios,focus,uitextfield,uialertview,Ios,Focus,Uitextfield,Uialertview,我试图将UIAlertView中的初始焦点设置为密码字段,但迄今为止我所做的一切都没有成功 我希望这段代码能起作用,但有一个错误我想不出来: - (void)showLoginFormWithUsername:(NSString*)username andPassword:(NSString*)password { UIAlertView *loginView = [[UIAlertView alloc] initWithTitle:@"Login"

我试图将UIAlertView中的初始焦点设置为密码字段,但迄今为止我所做的一切都没有成功

我希望这段代码能起作用,但有一个错误我想不出来:

- (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];
}

我做错了什么?

在尝试使警报视图成为响应者之前,您没有等待警报视图显示。相反,在
didPresentAlertView:
方法中执行此操作。

多么愚蠢的错误。>非常感谢!:)真倒霉。在iOS 8.3上,这似乎对我不起作用。