[英]Auto switching between UITextFields with Swift
I have 4 UITextFields
in one UIView
like this: 我在一个
UIView
有4个UITextFields
,如下所示:
each UITextField
limited to 4 characters. 每个
UITextField
限制为4个字符。 i want implement a auto switching between UITextFields
with count characters of each UITextField
. 我想实现之间的自动切换
UITextFields
与每个数字符UITextField
。
i use shouldChangeCharactersIn
for limitation characters: 我使用
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
}
and this is my switching implement: 这是我的切换工具:
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
}
}
}
my problem is switch to previous UITextField
in deleting texts, i can not detect backSpace event when UITextField
is empty. 我的问题是在删除文本时切换到以前的
UITextField
,当UITextField
为空时我无法检测到BackSpace事件。
Add target for textField in viewDidLoad() 在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)
Than create selector method 比创建选择器方法
func textFieldDidChange(textField: UITextField){ 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{
}
}
Make an outlet collection of all your textfields and configure them like so: 收集所有文本字段的出口,并按如下方式配置它们:
@IBOutlet var textfields: [UITextField]!
Add this in viewDidLoad:
func textFieldDidBeginEditing(_ textField: UITextField) {
textField.text = ""
}
In the target function, you'll restrict the count in this manner: 在目标函数中,将以这种方式限制计数:
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() } } }
This will the change in one of your delegate methods: 这将改变您的委托方法之一:
func textFieldDidBeginEditing(_ textField: UITextField) { textField.text = "" }
Refined version of the following answer: How to move cursor from one text field to another automatically in swift ios programmatically? 精炼版本的答案如下: 如何在ios中以编程方式自动将光标从一个文本字段移动到另一个文本字段?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.