0

我正在使用 BSKeyboard Controls 在用户名和密码的登录字段上的键盘上方有一个下一个/上一个和完成按钮。

我想要实现的是: - 当其中一个字段为空白时,完成按钮应该显示“完成” - 当两个字段至少有一个字符时,它应该显示“登录”

我知道有多种方法可以检查文本字段的内容,hasText isEqualToString !=nil 等。但我想我想在这里检查字符。

我需要知道放置 if 语句的最佳位置以及使用哪个位置。

我的领域是

self.usernameField
self.passwordField

我的键盘控件更新如下:

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

或者

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");
}
4

1 回答 1

0

您可以实现textField:shouldChangeCharactersInRange:replacementString:UITextFieldDelegate 以在用户输入键时执行您想要的任何操作。像这样的东西:

- (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");
    }
}
于 2013-02-20T22:06:13.630 回答