6

我有 4 个 uitextfields 可以识别它们何时为空或完整。如果完成,他们将返回键类型更改为 GO,否则为默认值。问题是即使我使用 reloadinputview 键盘也没有改变键类型

- (void)viewDidLoad
{
    [super viewDidLoad];

    _fieldsArray = @[_nameField, _passwordField, _emailField, _usernameField];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    NSRange textFieldRange = NSMakeRange(0, [textField.text length]);

    //NSLog(@"%d", !(NSEqualRanges(range, textFieldRange) && [string length] == 0));

    [self signUpFieldsAreValid:(!(NSEqualRanges(range, textFieldRange) && [string length] == 0) && [self validateSignUpFields:textField])];
    [textField reloadInputViews];
    return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{

    [self signUpFieldsAreValid:NO];
    [textField reloadInputViews];
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    for (UITextField *aTextField in _fieldsArray) {
        if (aTextField.isFirstResponder) {

            aTextField.layer.borderWidth = 0.f;
            aTextField.layer.borderColor = nil;

        }
    }

    textField.layer.borderWidth = 1.f;
    textField.layer.borderColor = [UIColor colorWithRed:200.f/255.f green:0.f/255.f blue:4.f/255.f alpha:1.f].CGColor;

    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSUInteger fieldIndex = [_fieldsArray indexOfObject:textField];

    [_fieldsArray[(fieldIndex + 1) % 4] becomeFirstResponder];

    return YES;
}

- (BOOL)validateSignUpFields:(UITextField *)firstResponder
{
    for (UITextField *aTextField in _fieldsArray) {
        if (!aTextField.text.length && ![aTextField isEqual:firstResponder]) {
            return NO;
        }
    }

    return YES;
}

- (void)signUpFieldsAreValid:(BOOL)valid
{
    NSLog(@"%d", valid);
    for (UITextField *aTextField in _fieldsArray) {

        if (valid) {
            aTextField.returnKeyType = UIReturnKeyGo;
        }
        else {
            aTextField.returnKeyType = UIReturnKeyDefault;
        }
    }
}
4

5 回答 5

16

我在更改 returnKeyType 后使用了以下内容,它似乎在 iOS7 和 iOS6.1 中运行良好:

if ([self.textfieldOne isFirstResponder]) {
    [self.textfieldOne reloadInputViews];

}
于 2013-12-11T04:23:28.673 回答
3

尝试这个。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{
    NSString *newStr=[textField.text stringByReplacingCharactersInRange:range withString:string];
    if([newStr length] > 0)
    {
        textField.returnKeyType = UIReturnKeySearch;
    }
    else
    {
        textField.returnKeyType = UIReturnKeyDefault;
    }
    [textField resignFirstResponder];
    [textField becomeFirstResponder];
    return YES;
}
于 2016-05-31T06:58:26.017 回答
2

根据文档,reloadInputViews唯一影响自定义输入视图,而不是标准键盘。

我为解决这个问题所做的就是调用resignFirstResponderthen becomeFirstResponder。这将在没有任何动画的情况下更新键盘:

代替:

[textFiled reloadInputViews];

做:

[textField resignFirstResponder];
[textField becomeFirstResponder];
于 2013-09-01T05:26:28.267 回答
1

在斯威夫特 5

 // Change returnKeyType based on textField text
func textFieldDidChangeSelection(_ textField: UITextField) {
    // Change return key to Done when text entered
    if textField.text != "" {
        textField.returnKeyType = .done
        textField.reloadInputViews()
    }
    // Change back to default style if all text removed
    else {
        textField.returnKeyType = .default
        textField.reloadInputViews()
    }
于 2020-12-20T15:34:03.677 回答
0

我在更改 returnKeyType 后使用了以下内容,它似乎在 iOS7 和 iOS6.1 中运行良好:

if ([self.textfieldOne isFirstResponder]) {
    [self.textfieldOne reloadInputViews];

}
于 2016-05-31T10:10:09.440 回答