我正在尝试将 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];
}
我究竟做错了什么?