我正在开发一个应用程序,在该应用程序中,我必须通过6个textField传递6位数的OTP,在其中您只能提供一个字符,然后它会自动转到另一个textField。我创建了6个textFields插座,并使用了此代码。我的问题是我想更改特定的textField
OTP号码,但是问题是,如果我想在点击十字按钮后更改文本字段4的OTP号码,它会自动转到文本字段3。解决这个问题?
我使用的代码:
import UIKit
class OneTimePasswordViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var txtOTP1: UITextField!
@IBOutlet weak var txtOTP2: UITextField!
@IBOutlet weak var txtOTP3: UITextField!
@IBOutlet weak var txtOTP4: UITextField!
@IBOutlet weak var txtOTP5: UITextField!
@IBOutlet weak var txtOTP6: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
txtOTP1.delegate = self
txtOTP2.delegate = self
txtOTP3.delegate = self
txtOTP4.delegate = self
txtOTP5.delegate = self
txtOTP6.delegate = self
self.txtOTP1.becomeFirstResponder()
}
}
这是创建逻辑的代码:
func textField(_ textField: UITextField, shouldChangeCharactersIn range:NSRange, replacementString string: String) -> Bool {
if (range.length == 0){
if textField == txtOTP1 {txtOTP2?.becomeFirstResponder()}
if textField == txtOTP2 {txtOTP3?.becomeFirstResponder()}
if textField == txtOTP3 {txtOTP4?.becomeFirstResponder()}
if textField == txtOTP4 {txtOTP5?.becomeFirstResponder()}
if textField == txtOTP5 {txtOTP6?.becomeFirstResponder()}
if textField == txtOTP6 {txtOTP6?.resignFirstResponder()}
textField.text? = string
return false
}
else if (range.length == 1) {
if textField == txtOTP6 {txtOTP5?.becomeFirstResponder()}
if textField == txtOTP5 {txtOTP4?.becomeFirstResponder()}
if textField == txtOTP4 {txtOTP3?.becomeFirstResponder()}
if textField == txtOTP3 {txtOTP2?.becomeFirstResponder()}
if textField == txtOTP2 {txtOTP1?.becomeFirstResponder()}
if textField == txtOTP1 {txtOTP1?.resignFirstResponder()}
textField.text? = ""
return false
}
return true
}
答案 0 :(得分:0)
您可以尝试“我的第三方”图书馆: https://github.com/Datt1994/DPOTPView
对于解决方案:-
在viewDidLoad中为所有textfield
添加标签和委托
import UIKit
class OneTimePasswordViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var txtOTP1: UITextField!
@IBOutlet weak var txtOTP2: UITextField!
@IBOutlet weak var txtOTP3: UITextField!
@IBOutlet weak var txtOTP4: UITextField!
@IBOutlet weak var txtOTP5: UITextField!
@IBOutlet weak var txtOTP6: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
txtOTP1.delegate = self
txtOTP1.tag = 1000
txtOTP2.delegate = self
txtOTP2.tag = 2000
txtOTP3.delegate = self
txtOTP3.tag = 3000
txtOTP4.delegate = self
txtOTP4.tag = 4000
txtOTP5.delegate = self
txtOTP5.tag = 5000
txtOTP6.delegate = self
txtOTP6.tag = 6000
self.txtOTP1.becomeFirstResponder()
}
}
在UITextFieldDelegate扩展中,实现如下的shouldChangeCharactersIn
函数,它也可以与textField.textContentType = .oneTimeCode
一起使用
extension OneTimePasswordViewController : UITextFieldDelegate {
public 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 < count*1000 {
let next = textField.superview?.viewWithTag((textField.tag/1000 + 1)*1000)
next?.becomeFirstResponder()
} else if textField.tag == count*1000 {
textField.resignFirstResponder()
}
} else if string.count == 0 { // is backspace
textField.text = ""
}
return false
}
}