大佬教程收集整理的这篇文章主要介绍了我如何将短信中的代码自动填充到 6 个 UITextfields 中,每个 UITextfields 包含 1 个数字,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如何将短信中的代码自动填充到 6 个 UITextfIElds 中,每个 UITextfIElds 包含 1 个数字?
首先,如果您有一个 UITextfield
数组,则需要为每个 UITextfields
分配一个标签,如下所示。
for i in 1 ... textFields.count {
textFields[i].tag = i
textFields[i].delegate = self
textFields[i].textContentType = .oneTimeCode
}
之后,您必须使用 UITextfield
委托方法之一 shouldChangeCharactersIn
。
func textField(_ textField: UITextField,shouldChangeCharactersIn range: NSRange,replacementString String: String) -> Bool {
if String.trimmingCharacters(in: CharacterSet.whitespaces).count != 0 {
textField.text = String
if textField.tag < textFields.count {
let next = textField.superview?.viewWithTag(textField.tag + 1)
next?.becomeFirstResponder()
} else if textField.tag == textFields.count {
textField.resignFirstResponder()
}
} else if String.count == 0 { // is BACkspace
textField.text = ""
}
return false
}
如果您需要这个场景的示例和最佳解决方案,我强烈推荐我自己的 Github 存储库:https://github.com/Datt1994/DPOTPView
以上是大佬教程为你收集整理的我如何将短信中的代码自动填充到 6 个 UITextfields 中,每个 UITextfields 包含 1 个数字全部内容,希望文章能够帮你解决我如何将短信中的代码自动填充到 6 个 UITextfields 中,每个 UITextfields 包含 1 个数字所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。