编辑时更改uitextfield返回键类型 [英] Changing uitextfield return key type while editing

查看:104
本文介绍了编辑时更改uitextfield返回键类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 4 uitextfields that recognize when they are empty or complete. If complete they change their return key type to GO, else is the default one. The problem is the keyboard is not changing the key type even though i use the 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;
        }
    }
}


推荐答案

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

I used the following after changing the returnKeyType and it seemed to work very well in iOS7 and iOS6.1:

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

}

这篇关于编辑时更改uitextfield返回键类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆