达到字符限制时切换到下一个UITextField

时间:2017-02-04 02:20:09

标签: swift string uitextfield

我有三个文本字段要输入电话号码。我试图将每个文本字段的字符限制设置为三个字符,一旦达到此字符,请切换到新的文本字段。

我在网上看到使用此代码来限制字符:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    let currentCharacterCount = textField.text?.characters.count ?? 0
    if (range.length + range.location > currentCharacterCount){
        return false
    }
    let newLength = currentCharacterCount + string.characters.count - range.length
    return newLength <= 25
}

并使用此方法切换到在新文本字段上键入:

.didbecomefirstresponder()

但我不确定如何将文本字段限制为3个字符,然后切换到下一个字段。

2 个答案:

答案 0 :(得分:2)

这是我的代码,我如何解决这个问题:

三个文本域是:

@IBOutlet var txtField1: UITextField!
@IBOutlet var txtField2: UITextField!
@IBOutlet var txtField3: UITextField!

UITextFieldDelegate导入您的班级,并将代理设置为自己的所有文本字段。

并使用此方法更改焦点。

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    let currentCharacterCount = ((textField.text?.characters.count)! + string.characters.count) - 1

    switch (textField, currentCharacterCount) {
    case (self.txtField1, 3):
        self.txtField2.becomeFirstResponder()
    case (self.txtField2, 7):
        self.txtField3.becomeFirstResponder()
    default:
        break
    }

    return true
}

这里我为第一个文本字段设置字符数3,为第二个文本字段设置7。

答案 1 :(得分:1)

您可以使用UITextField shouldChangeCharactersInRange的委托方法。但是你必须做一些小的设置工作。这是一个创建3个textFields的示例,符合UITextFieldDelegate,并执行类似于您所描述的内容。

class ViewController: UIViewController, UITextFieldDelegate {


var max = 3
var fields:[UITextField] = []

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    // This is just an example to layout 3 text fields.
    for i in 0 ..< 3 {
        let field = UITextField()
        view.addSubview(field)
        field.delegate = self
        field.borderStyle = .roundedRect
        field.font = UIFont(name: "AvenirNext-Regular", size: 15.0)
        field.textColor = UIColor.black
        field.frame = CGRect(x: view.bounds.width/2 - 100, y: 100 + (150*CGFloat(i)), width: 200, height: 50)
        fields.append(field)
    }


}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    // Make sure the field has non-nil text
    if let text = textField.text {
        // Check if the text length has reached max limit
        if text.characters.count > (max-1) {
            // Get index of field from fields array
            if let index = fields.index(of: textField) {
                // Make sure it's not the last field in the array else return false
                if index < fields.count-1 {
                    // Make the next field in the array become first responder if it's text is non-nil and it's text's character count is less than desired max
                    if fields[index+1].text != nil && fields[index+1].text!.characters.count < (max-1) {
                        fields[index+1].becomeFirstResponder()
                    } else {
                        return false
                    }
                } else {
                    return false
                }
            }
        }
    }
    return true
}

}