使用Swift在UITextFields之间自动切换

时间:2017-01-28 17:29:48

标签: ios iphone swift uitextfield uitextfielddelegate

我在一个UITextFields中有4个UIView,如下所示:

enter image description here

每个UITextField限制为4个字符。我希望实现UITextFields之间的自动切换,每个UITextField的计数字符。

我使用shouldChangeCharactersIn作为限制字符:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let maxLength = 4
        var newString = String()
        let currentString: NSString = textField.text! as NSString
        newString = currentString.replacingCharacters(in: range, with: string)

        return newString.length <= maxLength
    }

这是我的切换工具:

func textFieldDidChange(_ textField: UITextField){

    let text = textField.text

    if text?.utf16.count == 4 {
        switch textField {
        case firstPartTextField:
            secondPartTextField.becomeFirstResponder()
        case secondPartTextField:
            thirdPartTextField.becomeFirstResponder()
        case thirdPartTextField:
            fourthPartTextField.becomeFirstResponder()
        default:
            break
        }
    }
}

我的问题是在删除文本时切换到上一个UITextField,当UITextField为空时我无法检测到backSpace事件。

2 个答案:

答案 0 :(得分:1)

在viewDidLoad()

中添加textField的目标
firstTxt.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
    secTxt.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
    thirdTxt.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
    fourTXt.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)

比创建选择器方法

func textFieldDidChange(textField:UITextField){

    let text = textField.text

    if text?.utf16.count == 1{
        switch textField{
        case firstTxt:
            secTxt.becomeFirstResponder()
        case secTxt:
            thirdTxt.becomeFirstResponder()
        case thirdTxt:
            fourTXt.becomeFirstResponder()
        case fourTXt:
            fourTXt.resignFirstResponder()
        default:
            break
        }
    }else{

    }
}

答案 1 :(得分:0)

制作所有文本字段的出口集合,并按如下方式对其进行配置:

OutletCollection:
{
    "error": {
        "code": "CP_SI_ValidationFailed",
        "message": "email and password is required"
    }
}
在viewDidLoad中添加:
@IBOutlet var textfields: [UITextField]!

在目标函数中,您将以这种方式限制计数:

for (i, tf) in textfields.enumerated() {
    tf.layer.cornerRadius = 5
    tf.clipsToBounds = true
    tf.tag = i
    tf.delegate = self
}

这将改变您的一个委托方法:

func textFieldDidChange(textField: UITextField) {
       let text = textField.text!
       if text.utf16.count >= 4 {
           if textField.tag < 4 {
               codeTextfields[textField.tag + 1].becomeFirstResponder()
           }
           else {
               textField.resignFirstResponder()
           }
       }
}

以下答案的精炼版:How to move cursor from one text field to another automatically in swift ios programmatically?