用户在Swift中键入2个字符时,将焦点设置在下一个文本字段上

时间:2016-03-02 10:41:41

标签: xcode swift focus

我迫切地想找一个能回答我问题的人。 我有一个包含4个TextFields的表单。我希望当用户键入2个字符时,焦点会自动设置在下一个TextField上。

我尝试过以下代码:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    guard let text = textField.text else { return true }
    //let newLength = text.characters.count + string.characters.count - range.length


    if (text.characters.count == 2 && textField.tag == 10){
        textField.resignFirstResponder()
        self.txt_min_debut.becomeFirstResponder()
        return false
    }

    else if (text.characters.count == 2 && textField.tag == 11){
        textField.resignFirstResponder()
        self.txt_heure_fin.becomeFirstResponder()
        return false
    }

    else if (text.characters.count == 2 && textField.tag == 12){
        textField.resignFirstResponder()
        self.txt_min_fin.becomeFirstResponder()
        return false
    }

    else if (text.characters.count == 2 && textField.tag == 13){
        textField.resignFirstResponder()
        self.txt_cr.becomeFirstResponder()
        return false
    }

它不能像我想的那样工作。当我键入2个字符时,焦点仍然在同一个TextField上。我必须输入第3个字符才能继续下一个字符。 另一个问题是del键被识别为键。这意味着,当我键入2个字符时,我无法再次编辑它。

我正在使用Xcode和Swift 2。

4 个答案:

答案 0 :(得分:3)

更新了SWIFT3的代码

@IBOutlet weak var tf1: UITextField!
@IBOutlet weak var tf2: UITextField!
@IBOutlet weak var tf3: UITextField!
@IBOutlet weak var tf4: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()
    tf1.delegate = self
    tf2.delegate = self
    tf3.delegate = self
    tf4.delegate = self


    tf1.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
    tf2.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
    tf3.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
    tf4.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)

}
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    tf1.becomeFirstResponder()

}
func textFieldDidChange(textField: UITextField){

    let text = textField.text

    if text?.utf16.count==2{
        switch textField{
        case tf1:
            tf2.becomeFirstResponder()
        case tf2:
            tf3.becomeFirstResponder()
        case tf3:
            tf4.becomeFirstResponder()
        case tf4:
            tf4.resignFirstResponder()
        default:
            break
        }
    }else{

    }
}

答案 1 :(得分:0)

Try this code.

 @IBOutlet weak var textFrst: UITextField!
 @IBOutlet weak var textSecond: UITextField!
 @IBOutlet weak var textThird: UITextField!
 @IBOutlet weak var textFourth: UITextField!

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

        let currentString: NSString = textField.text!
        let newString: NSString =
        currentString.stringByReplacingCharactersInRange(range, withString: string)
        let newLength: Int = newString.length
        if textField == textFrst {
            if newLength == 3 {
                textSecond.becomeFirstResponder()
            }
        }
        if textField == textSecond{
            if newLength == 3 {
                textThird.becomeFirstResponder()
            }
        }
        if textField  == textThird {
            if newLength == 3 {
                textFourth.becomeFirstResponder()
            }
        }
        if textField == textFourth {
            if newLength == 3 {
               self.view.endEditing(true)
            }
        }
        return true
    }

答案 2 :(得分:0)

我有一个类似的问题并通过简单地将textField.text与替换字符串连接来解决它:

let enteredText = textField.text! + string

然后逻辑如"如果输入了Text.characters.count == 2"应该按预期工作。

答案 3 :(得分:0)

感谢你们两位。

我测试了这两个解决方案,Ben的一个没有工作,它在TextField中只保留了1个char而不是2个。

Bhagyalaxmi的解决方案似乎有效,除了光标没有在视觉上"移动到下一个TextField(但是当你写一个char时,它就会出现在好地方)。