2

In the following code I am trying to transfer control from UITextField to the next via a next button.

I am doing this by calling becomeFirstResponder on the next UITextField.

If I don't type anything in the first and current UITextField the next button works as expected. The keyboard stays up and the focus is transferred.

If I do type something, and only if the field is empty. The method becomeFirstResponder for the next field is called and returns true, yet the keyboard is dismissed and focus is not transferred.

public func numberPad(numberPad: APNumberPad, functionButtonAction:UIButton, textInput: UIResponder) {

    var current:UITextField?

    for field in editCells {

        if (current != nil) {

            field.valueTextField.becomeFirstResponder()

            return;
        }

        if (field.valueTextField == activeField) {
            current = field.valueTextField
        }

    }

    textInput.resignFirstResponder()


}

This function is called when the NEXT or DONE button is pressed on the keyboard. Which is a custom number keypad. APNumberPad specifically. https://github.com/podkovyrin/APNumberPad It is my delegate function.

Anyone know any reason becomeFirstResponder would return true and not work, only in some cases, but work in others?

And yes this is the main UI thread. Adding a call to resignFirstResponder on the current field, then a delay and calling becomeFirstResponder works. This causes the keypad to flicker, no matter how small the delay though.

Edit... I am now doing this... and am living with the keyboard flicker for now: Delay is a helper function for GCD

public func numberPad(numberPad: APNumberPad, functionButtonAction:UIButton, textInput: UIResponder) {

    var current:UITextField?

    for field in editCells {

        if (current != nil) {

            current?.resignFirstResponder()
            delay (0) {
                field.valueTextField.becomeFirstResponder()
            }

            return;
        }

        if (field.valueTextField == activeField) {
            current = field.valueTextField
        }

    }

    textInput.resignFirstResponder()


}
1

1 Answer 1

0

I don't know if it helps you, or not. I wrote a simple UITextField extension that contains a returnView variable which decides what the textfield should do on return key press:

  • turn to next text field (if the returnView is an UITextField)
  • simulate button touch (if the returnView is a UIButton)
  • or hide keyboard
class NextTextField: UITextField, UITextFieldDelegate {

    @IBOutlet weak var returnView: UIView? {
        didSet {
            if returnView is UITextField {
                returnKeyType = UIReturnKeyType.Next
            }
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()

        delegate = self
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {

        if let nextTextField = self.returnView as? UITextField {
            nextTextField.becomeFirstResponder()
        } else if let nextButton = self.returnView as? UIButton {
            nextButton.sendActionsForControlEvents(.TouchUpInside)
        } else {
            self.resignFirstResponder()
        }

        return true
    }

}
1
  • While that is a nice refactor, I've confirmed via debugging becomeFirstResponder is always being called and always returns true. Oct 27, 2015 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.