gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:51:44 26 4
gpt4 key购买 nike

我正在拼命寻找可以回答我问题的人。我有一个包含 4 个 TextField 的表单。我希望当用户键入 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。

最佳答案

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{

}
}

关于xcode - 当用户在 Swift 中输入 2 个字符时将焦点设置在下一个文本字段上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35744854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com