Jake Ortiz
Jake Ortiz

Reputation: 513

Changing uitextfield return key type while editing

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;
        }
    }
}

Upvotes: 6

Views: 3802

Answers (5)

TripleMonkey
TripleMonkey

Reputation: 11

In Swift 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()
    }

Upvotes: 1

vivek tankariya
vivek tankariya

Reputation: 156

Try this.

- (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;
}

Upvotes: 3

Alex_Burla
Alex_Burla

Reputation: 810

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];

}

Upvotes: 0

wilforeal
wilforeal

Reputation: 369

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];

}

Upvotes: 16

rmaddy
rmaddy

Reputation: 318934

According to the docs, the reloadInputViews only affects custom input views, not the standard keyboard.

What I've done to solve this problem is to call resignFirstResponder then becomeFirstResponder. This will update the keyboard without any animation:

Instead of:

[textFiled reloadInputViews];

do:

[textField resignFirstResponder];
[textField becomeFirstResponder];

Upvotes: 2

Related Questions