gpt4 book ai didi

ios - 文本字段退格操作识别文本字段何时为空?

转载 作者:可可西里 更新时间:2023-10-31 23:57:22 27 4
gpt4 key购买 nike

我正在尝试使用五个文本字段创建 otp 文本字段。如果添加顶部,一切正常,但是当用户尝试添加空文本字段并尝试退格并且它没有调用我已经调用的 UItextfiled 的任何委托(delegate)方法时出现问题添加。

我试过这个:-

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

let char = string.cStringUsingEncoding(NSUTF8StringEncoding)!
let isBackSpace = strcmp(char, "\\b")

if (isBackSpace == -92) {
println("Backspace was pressed")
}
return true
}

但当文本字段不为空时调用。

例如:-

在下面的屏幕截图中添加 1 和两个不同的文本字段,第三个是空的但是当我尝试退格时它需要进入第二个文本字段(第三个是空的)这就是我面临的问题。

谢谢

enter image description here

最佳答案

随后@Marmik Shah 和@Prashant Tukadiya 在这里回答我添加我的答案,为了快速回答我从 here 中获取了一些代码

第一步:

为所有文本字段创建 IBOutletCollection,并且不要忘记按顺序在所有文本字段中设置标签,例如 [1,2,3,4,5,6]

class ViewController: UIViewController{

@IBOutlet var OTPTxtFields: [MyTextField]! // as well as set the tag for textfield in the sequence order

override func viewDidLoad() {
super.viewDidLoad()

//change button color and other options
OTPTxtFields.forEach { $0.textColor = .red; $0.backspaceTextFieldDelegate = self }
OTPTxtFields.first.becomeFirstResponder()
}

第 2 步:

在您当前页面的 UITextField 委托(delegate)方法中

extension ViewController : UITextFieldDelegate, MyTextFieldDelegate {

func textFieldDidEnterBackspace(_ textField: MyTextField) {
guard let index = OTPTxtFields.index(of: textField) else {
return
}

if index > 0 {
OTPTxtFields[index - 1].becomeFirstResponder()
} else {
view.endEditing(true)
}
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let newString = ((textField.text)! as NSString).replacingCharacters(in: range, with: string)


if newString.count < 2 && !newString.isEmpty {
textFieldShouldReturnSingle(textField, newString : newString)
// return false
}

return newString.count < 2 || string == ""
//return true
}
override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(copy(_:)) || action == #selector(paste(_:)) {
return false
}

return true
}
func textFieldShouldReturnSingle(_ textField: UITextField, newString : String)
{
let nextTag: Int = textField.tag + 1
textField.text = newString
let nextResponder: UIResponder? = textField.superview?.viewWithTag(nextTag)
if let nextR = nextResponder
{
// Found next responder, so set it.

nextR.becomeFirstResponder()
}
else
{
// Not found, so remove keyboard.
textField.resignFirstResponder()
callOTPValidate()
}
}

}

第 3 步:

创建用于访问向后函数的文本字段类

class MyTextField: UITextField {
weak var myTextFieldDelegate: MyTextFieldDelegate?

override func deleteBackward() {
if text?.isEmpty ?? false {
myTextFieldDelegate?.textFieldDidEnterBackspace(self)
}

super.deleteBackward()
}

protocol MyTextFieldDelegate: class {
func textFieldDidEnterBackspace(_ textField: MyTextField)
}

步骤 - 4

最后按照@Marmik Shah 的回答为您的 UITextField 自定义类

第 5 步

使用这个从每个文本字段获取值

func callOTPValidate(){
var texts: [String] = []
OTPTxtFields.forEach { texts.append($0.text!)}
sentOTPOption(currentText: texts.reduce("", +))

}

func sentOTPOption(currentText: String) {
print("AllTextfieldValue == \(currentText)")
}

关于ios - 文本字段退格操作识别文本字段何时为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53665104/

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