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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35744854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:36:11  来源:igfitidea点击:

Set focus on a next textfield when user has typed 2 characters in Swift

xcodeswiftfocus

提问by Krystal

I'm desperately looking for someone who could answer my question. I have a form with 4 TextFields. I want that when the user has typed 2 chars, the focus is automatically set on the next TextField.

我正在拼命寻找可以回答我问题的人。我有一个包含 4 个文本字段的表单。我希望当用户输入 2 个字符时,焦点会自动设置在下一个 TextField 上。

I've tried the following code :

我试过以下代码:

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
    }

It doesn't work like I want. When I type 2 chars, the focus is still on the same TextField. I have to type on a 3rd char to move on the next one. Another problem is that the del key is recognized as a key. That means, when I type 2 chars, I can't edit it again.

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

I'm working with Xcode and Swift 2.

我正在使用 Xcode 和 Swift 2。

采纳答案by Bhagyalaxmi Poojary

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
    }

回答by Chetan Rajagiri

Updated Code for SWIFT3

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{

    }
}

回答by Ben Sullivan

I had a similar issue and resolved it by simply concatenating the textField.text with the replacement string as such:

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

let enteredText = textField.text! + string

Then logic such as "if enteredText.characters.count == 2" should work as expected.

然后诸如“if enterText.characters.count == 2”之类的逻辑应该按预期工作。

回答by Krystal

Thanks to both of you.

感谢你们俩。

I tested the two solutions, and Ben's one was not working, it keeps only 1 char instead of 2 in the TextField.

我测试了这两种解决方案,Ben 的一种不起作用,它在 TextField 中只保留了 1 个字符而不是 2 个字符。

Bhagyalaxmi's solution seems to work, except that the cursor doesn't "visually" move to the next TextField (but when you write a char, it goes at the good place).

Bhagyalaxmi 的解决方案似乎有效,只是光标不会“在视觉上”移动到下一个 TextField(但是当你写一个字符时,它会在好地方)。