Esko918
Esko918

Reputation: 1469

Changing the return key type while still editing UITextField?

IM having an issue with changing the return keys type while still editing the textfield of my view. So when the user clicks the textfield i have a few checks that change the return type of the keyboard, these checks are placed in - (void)textFieldDidBeginEditing:(UITextField *)textField, when i change the keyboards return key here the code works fine. Im just changing the keyboards retrun key like so [textField setReturnKeyType:UIReturnKeyDone];. So there are 2 textfields username and password. If theres text in both the textfields i want to change the return key to DONE if theres text in only one of the text fields i want to change it to NEXT so i can jump back and forth between the textfields.So now im placing some checks and balances in - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string. Yet the same code im placing in textFieldDidBeginEditing is not working in this function. Ive read a bunch of posts and they all say to remove the text field as the first responder then set the return key and become the responder again, but this messes up my current flow in the respective methods. I also read that i have to call [textfield reloadInputFields] after setting the return key, yet again this does not work. You would think that apple would have fixed a bug like this by now. Anyway does anyone have some input here?

Upvotes: 3

Views: 2058

Answers (2)

Christian Schober
Christian Schober

Reputation: 185

I had to implement an add tag logic that required to change the keyboard return key depending on the text typed into the UITextField. Finding how to change the return key was quite easy, but when I started testing, I noticed that this solution only works for iOS10, but not for iOS9. After spending some time and doing some research, I came up with this workaround, it's not the nicest but at least it works.

if #available(iOS 10.0, *) {
    textField.reloadInputViews()
} else {
    textField.keyboardType = .numberPad
    textField.reloadInputViews()
    textField.keyboardType = .default
    textField.reloadInputViews()
}

Upvotes: 6

Mubin Mall
Mubin Mall

Reputation: 546

Try it out and Let me know

I have made demo for your question and its running perfect as per your need or you can modify it as per

// This is the Editing Changed Action Event of Both textField Created From StoryBoard

- (IBAction)txtEditingChanged:(UITextField *)sender
{
    [sender resignFirstResponder];
    [sender becomeFirstResponder];
}

// This is the Editing Did End Action Event of Both textField Created From StoryBoard

- (IBAction)txtEditingEnd:(UITextField *)sender
{
    if (txtUserName.text.length == 0 || txtPassword.text.length == 0)
    {
        [txtUserName setReturnKeyType:UIReturnKeyNext];
        [txtPassword setReturnKeyType:UIReturnKeyNext];
    }
    else
    {
        [txtUserName setReturnKeyType:UIReturnKeyDone];
        [txtPassword setReturnKeyType:UIReturnKeyDone];
    }
}

// This is Next And Done Button Events i.e. UITextField Delegate

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == txtUserName && textField.returnKeyType == UIReturnKeyNext && textField.text.length != 0)
    {
        [txtPassword becomeFirstResponder];
    }
    else if (textField == txtPassword && textField.returnKeyType == UIReturnKeyNext && textField.text.length != 0)
    {
        [txtUserName becomeFirstResponder];
    }
    else
    {
        // This is your Done Button Event You Can Procced Here
    }
    return YES;
}

Upvotes: 1

Related Questions