Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do something when click on Return Button #426

Closed
nahouto opened this issue Feb 2, 2016 · 5 comments
Closed

Do something when click on Return Button #426

nahouto opened this issue Feb 2, 2016 · 5 comments
Labels

Comments

@nahouto
Copy link

nahouto commented Feb 2, 2016

Hi,

I don't understand how to do something when the user press on return button.
I tried that in viewDidLoad :

    returnKeyHandler = IQKeyboardReturnKeyHandler(controller: self)
    emailTextField.delegate = returnKeyHandler
    passwordTextField.delegate = returnKeyHandler
    self.view.setCustomDoneTarget(self, selector: "loginButton:")

But it doesn't work...

Something else, I am confused about the custom Done Button (in the bar above the keyboard) and the keyboard Return button (bottom right).

Thank your for this pod !

@hackiftekhar
Copy link
Owner

For the return button. Either you can use IQKeyboardReturnHandler, and set it's delegate to self. You don't need to set email, password textfield delegate to returnKeyHandler. Now you can implement textfieldShouldReturn and you can do your custom work there.

CustomDoneTarget is a different thing and doesn't bound with return key.

@DmitryArbuzov
Copy link

It doesn't work. I tried to use your keyboardManager for tableView with textViews in cells. Arrow button works perfectly, but return key doesn't work as expected.
I was forced to implement it manually:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if([text isEqualToString:@"\n"]) {
        IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager];
        if (keyboardManager.canGoNext) {
            [keyboardManager goNext];
        } else {
            [keyboardManager resignFirstResponder];
        }
        return NO;
    }

    return YES;
}

@nahouto
Copy link
Author

nahouto commented Feb 3, 2016

I tried that :

    var returnKeyHandler: IQKeyboardReturnKeyHandler!
    override func viewDidLoad() {
        returnKeyHandler = IQKeyboardReturnKeyHandler(controller: self)
        returnKeyHandler.delegate = self
    }

    // do when click on bottom right button (next button or done button)
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        switch textField {
            // move to the next text field
        case emailTextField:
            passwordTextField.becomeFirstResponder()
            // press the final button
        case passwordTextField:
            loginButton.sendActionsForControlEvents(UIControlEvents.TouchUpInside)
        default: break
        }
        return true
    }

but the textfields do not respond at all ! it is impossible to type anything in them.

Finally, I did it manually :

    override func viewDidLoad() {
        emailTextField.delegate = self
        passwordTextField.delegate = self
    }

    // do when click on bottom right button (next button or done button)
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        switch textField {
            // move to the next text field
        case emailTextField:
            passwordTextField.becomeFirstResponder()
            // press the final button
        case passwordTextField:
            loginButton.sendActionsForControlEvents(UIControlEvents.TouchUpInside)
        default: break
        }
        return true
    }

and it works as expected

@mehulparmar4ever
Copy link

Write something like this, And It will work Perfectly.

extension LoginVC : UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}

@Enriquecm
Copy link

You just need to add this code on the textFieldShouldReturn method:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  if IQKeyboardManager.shared.canGoNext {
    IQKeyboardManager.shared.goNext()
  } else {
    textField.resignFirstResponder()
  }
  return true
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants