[[如果所有文本字段都被填充,它会很容易返回,但是如果所有文本字段都为空,并且我单击上一个文本字段,那么我想返回上一个文本字段,以此类推,请问如何帮助我]
import UIKit
class OTPVC: CommonViewController,UITextFieldDelegate {
@IBOutlet weak var aboutOtp: UIView!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
@IBOutlet weak var firstOtptxt: UITextField!
@IBOutlet weak var secondOtptxt: UITextField!
@IBOutlet weak var thirdOtptxt: UITextField!
@IBOutlet weak var fourthOtptxt: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
setUpView()
firstOtptxt.delegate = self
secondOtptxt.delegate = self
thirdOtptxt.delegate = self
fourthOtptxt.delegate = self
}
private func setUpView() {
aboutOtp.makeCornerRadius(10)
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if ((textField.text?.count)!) < 1 && (string.count > 0) {
if textField == firstOtptxt{
secondOtptxt.becomeFirstResponder()
}
if textField == secondOtptxt {
thirdOtptxt.becomeFirstResponder()
}
if textField == thirdOtptxt {
fourthOtptxt.becomeFirstResponder()
}
if textField == fourthOtptxt {
fourthOtptxt.becomeFirstResponder()
}
textField.text = string
return false
} else if (((textField.text?.count)!) >= 1) && (string.count == 0) {
if textField == secondOtptxt {
firstOtptxt.becomeFirstResponder()
}
if textField == thirdOtptxt {
secondOtptxt.becomeFirstResponder()
}
if textField == fourthOtptxt {
thirdOtptxt.becomeFirstResponder()
}
if textField == firstOtptxt {
firstOtptxt.becomeFirstResponder()
}
textField.text = string
return false
}
else if ((textField.text?.count)!) >= 1 {
textField.text = string
return false
}
return true
}
}
[if all textfield is fill it will easily come back but if all textfield is empty and i click on last textfield then i want to come back to the previous textfield and so on how i achieved this please help me ]
[[如果所有文本字段都被填充,它会很容易返回,但是如果所有文本字段都为空,并且我单击上一个文本字段,那么我想返回上一个文本字段,以此类推,请问如何帮助我]
答案 0 :(得分:0)
答案 1 :(得分:0)
protocol MyTextFieldDelegate: UITextFieldDelegate {
func textField(_ textField: UITextField, didDeleteBackwardAnd wasEmpty: Bool)
}
class MyTextField: UITextField {
override func deleteBackward() {
// see if text was empty
let wasEmpty = text == nil || text! == ""
// then perform normal behavior
super.deleteBackward()
// now, notify delegate (if existent)
(delegate as? MyTextFieldDelegate)?.textField(self, didDeleteBackwardAnd: wasEmpty)
}
}
[只需在我的项目yippy中放置一个委托方法及其工作情况就很好了