如何在Swift

时间:2018-01-18 02:04:20

标签: ios swift uitextfield swift4

我有2个带有NumberPad键盘类型

的textFields
@IBOutlet weak var ourTextField: UITextField!
@IBOutlet weak var forThemTextField: UITextField!

我希望在myTextField中输入两个数字之后自动移动到另一个文本字段(从ourTextField到forTheTeTextField)然后转到另一个文本字段(forThemTextField)并输入2个数字我希望键盘自动隐藏

我添加了一个条件,只能通过以下代码在textFields中接受两个数字:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let lengthsDictionary = [ourTextField : 2, forThemTextField: 2]
    guard let length = lengthsDictionary[textField] else {
        return true
    }
    let currentCharacterCount = textField.text?.count ?? 0
    if (range.length + range.location > currentCharacterCount){
        return false
    }
    let newLength = currentCharacterCount + string.count - range.length

    return newLength <= length
}

3 个答案:

答案 0 :(得分:4)

{{1}}

答案 1 :(得分:2)

使用shouldChangeCharactersIn方法的UITextFieldDelegate&amp;用键盘串听 使用textFields的标签,例如标签1&amp;标记2

作为

class TestingViewController: UIViewController{
    @IBOutlet weak var firstTextField: UITextField!
    @IBOutlet weak var secondTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        firstTextField.delegate = self
        secondTextField.delegate = self
        firstTextField.tag = 1
        secondTextField.tag = 2
        firstTextField.keyboardType = .numberPad
        secondTextField.keyboardType = .numberPad
    }
}

extension TestingViewController: UITextFieldDelegate {
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        return textField.shouldChangeCustomOtp(textField: textField, string: string)

    }
}

extension UITextField {
    func shouldChangeCustomOtp(textField:UITextField, string: String) ->Bool {

        //Check if textField has two chacraters
        if ((textField.text?.count)! == 1  && string.count > 0) {
            let nextTag = textField.tag + 1;
            // get next responder
            var nextResponder = textField.superview?.viewWithTag(nextTag);
            if (nextResponder == nil) {
                nextResponder = textField.superview?.viewWithTag(1);
            }

            textField.text = textField.text! + string;
            //write here your last textfield tag
            if textField.tag == 2 {
                //Dissmiss keyboard on last entry
                textField.resignFirstResponder()
            }
            else {
                ///Appear keyboard
                nextResponder?.becomeFirstResponder();
            }
            return false;
        } else if ((textField.text?.count)! == 1  && string.count == 0) {// on deleteing value from Textfield

            let previousTag = textField.tag - 1;
            // get prev responder
            var previousResponder = textField.superview?.viewWithTag(previousTag);
            if (previousResponder == nil) {
                previousResponder = textField.superview?.viewWithTag(1);
            }
            textField.text = "";
            previousResponder?.becomeFirstResponder();
            return false
        }
        return true

    }

}

<强>输出:

enter image description here

答案 2 :(得分:1)

将目标添加到UITextField(在viewDidLoad()中),例如:

mTfActuallyFrom?.addTarget(self, action: #selector(CalculatorViewController.textFieldDidChange(_:)), for: UIControlEvents.editingChanged)

在委托函数中,使用UITextField.becomeFirstResponder()将焦点移动到下一个文本字段。根据您的要求更改您的条件。例如:

func textFieldDidChange(_ textField: UITextField) {
        if textField == mTfActuallyFrom {
            if (textField.text?.characters.count)! >= 4 {
                mTfActuallyTo?.becomeFirstResponder()
            }
        }
        else if textField == mTfActuallyTo {
            if (textField.text?.characters.count)! >= 4 {
                dismissKeyboard(gesture: nil)
            }
        }
    }