2013-02-20 35 views
0

我正在使用BSKeyboard Controls在用戶名和密碼的登錄字段上的鍵盤上方顯示下一個/上一個和完成按鈕。UITextField和鍵盤控件檢查字段是否有數據

我想實現的是: - 當領域的一個空白完成按鈕應該說「完成」 - 當這兩個領域都至少有一個字符應該說「登錄」

我明白有多種方法來檢查文本字段內容,hasText isEqualToString!= nil等。但我想在這裏檢查字符我猜。

我需要知道什麼是放置if語句和使用哪個語句的最佳位置。

我的字段

self.usernameField 
self.passwordField 

我的鍵盤控制更新這樣的:

self.keyboardControls.doneTitle = NSLocalizedString(@"KeyboardControlsDone", @"test"); 

OR

self.keyboardControls.doneTitle = NSLocalizedString(@"KeyboardControlsLogin", @"test"); 

更新方法:

NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string]; 

UITextField *otherTextField; 
if (textField == self.passwordField) 
{ 
    otherTextField = self.usernameField; 
} 
else 
{ 
    otherTextField = self.passwordField; 
} 

if ([newText length] > 0 && [otherTextField.text length] > 0) 
{ 
    self.keyboardControls.doneTitle = NSLocalizedString(@"KeyboardControlsLogin",@"Button for Keyboard Controls on Login page"); 
} else { 
    self.keyboardControls.doneTitle = NSLocalizedString(@"KeyboardControlsDone", @"test"); 
} 

回答

0

您可以實現UITextFieldDelegate的textField:shouldChangeCharactersInRange:replacementString:,以便在用戶輸入密鑰時執行任何您想要的操作。 事情是這樣的:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
    NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string]; 

    UITextField *otherTextField; 
    if (textField == self.passwordField) 
    { 
     otherTextField = self.usernameField; 
    } 
    else 
    { 
     otherTextField = self.passwordField; 
    } 

    if ([newText length] > 0 && [otherTextField.text length] > 0) 
    { 
//  Your code 
    } 
    return YES; 
} 

編輯

而不是使用委託方法,使用事件編輯修改。您必須使用IB或代碼爲該事件設置操作,它看起來像這樣:

- (IBAction) textFieldEditingChanged 
{ 
    if ([self.usernameField.text length] > 0 && [self.passwordField.text length] > 0) 
    { 
     self.keyboardControls.doneTitle = NSLocalizedString(@"KeyboardControlsLogin",@"Button for Keyboard Controls on Login page"); 
    } else { 
     self.keyboardControls.doneTitle = NSLocalizedString(@"KeyboardControlsDone", @"test"); 
    } 
} 
+0

謝謝,我更新了我的問題文本。我現在遇到的困難是,如果你輸入用戶名,然後輸入密碼(這工作正常的按鈕改變登錄),但如果你然後清除密碼(或回到用戶名和清除用戶名),它不會改回完成。 – StuartM 2013-02-20 22:56:16

+1

你是對的,如果用戶清除文本字段,則委託方法不會被觸發,請參閱我的編輯中的解決方案。 – e1985 2013-02-21 00:36:35

+0

我實際上使用了uitextfield清除方法: - (BOOL)textFieldShouldClear:(UITextField *)textField然後將按鈕更改爲完成,要麼工作。謝謝 – StuartM 2013-02-21 12:30:57