[英]return being called twice in ios app
我正在嘗試使用用戶名和密碼文本字段在我的iOS應用中實現一個非常基本的登錄屏幕。
在用戶輸入用戶名后點擊“下一步”后,我查找了如何使視圖自動聚焦於密碼文本字段。 但是,由於某些原因,似乎在用戶輸入用戶名后兩次檢測到該返回。 而不是將鍵盤保持在屏幕上並專注於密碼textField,鍵盤消失了,並且沒有textField處於焦點。
我究竟做錯了什么?
LoginViewController.m:
@implementation LoginViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
username = [[NSString alloc]init];
keychainItem = [[KeychainItemWrapper alloc]initWithIdentifier:@"Login" accessGroup:nil];
}
// Check if user presses next from the username textfield -> automatically go to password
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == self.txtPassword) {
// try logging in
NSLog(@"trying to proceed to submit form");
} else if (theTextField == self.txtUsername) {
// automatically go to the password field
NSLog(@"trying to proceed to password field");
[self.txtPassword becomeFirstResponder];
}
return YES;
}
當我查看日志時,用戶在用戶名字段中單擊“下一步”后,將顯示以下兩個日志
2014-08-27 00:11:48.594 BiP[3049:60b] trying to proceed to password field
2014-08-27 00:11:48.640 BiP[3049:60b] trying to proceed to submit form
嘗試這個:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == self.txtPassword) {
// try logging in
NSLog(@"trying to proceed to submit form");
return YES;
} else if (theTextField == self.txtUsername) {
// automatically go to the password field
NSLog(@"trying to proceed to password field");
[self.txtPassword becomeFirstResponder];
return NO;
}
return YES;
}
希望這可以幫助.. :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.