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

textfield backspace action identify when textfield is empty?

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

我试过了:-

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 和两个不同的文本字段,第三个是空的但是当我尝试退格时它需要进入第二个文本字段(第三个是空的)这就是我面临的问题边.

谢谢

您可以覆盖函数 deleteBackward()
创建一个新的 Class 继承 UITextField 并触发和 EditingEnd 事件。

class MyTextField: UITextField {
    override func deleteBackward() {
        super.deleteBackward()
        print("Backspace");
        self.endEditing(true);
    }
}

然后,在您的 Storyboard 中,为 UITextField

添加自定义 class

接下来,在您的视图控制器中,在 editingEnd 操作中,您可以切换文本字段。为此,您需要为每个文本字段设置一个标签值。
例如,您有两个文本字段,tfOne 和 tfTwo。
tfOne.tag = 1; tfTwo.tag = 2
现在,如果当前您正在编辑 tfTwo 并单击退格键,则将当前编辑的文本字段设置为 tfOne

class ViewController: UIViewController {
    @IBOutlet weak var tfOne: MyTextField!
    @IBOutlet weak var tfTwo: MyTextField!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func editingEnded(_ sender: UITextField) {
        // UITextField editing ended
        if(sender.tag == 2) {
            self.tfOne.becomeFirstResponder();
        }
    }
}

您可以按 101,102,103,104,105 等顺序为您的文本字段添加标签。

点击退格键时。检查字符串的长度是否等于 0。然后转到 textfield.tag - 1 直到你得到第一个 textfield.like 如果你在文本字段 102 上,然后转到文本字段 102-1 = 101。

与输入任何字符转到下一个文本字段直到到达最后一个文本字段相同,就像如果您在文本字段 102 上然后转到文本字段 102+1 = 103。

您可以使用(self.view.viewWithTag(yourTag) as? UITextField)?.becomeFirstResponder()

我没有系统,所以无法post编码

随后@Marmik Shah 和@Prashant Tukadiya 在这里回答我添加我的答案,为了快速回答我从 here

中获取了一些代码

第 1 步:

为所有文本字段创建 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 { [=10=].textColor  = .red;  [=10=].backspaceTextFieldDelegate = self }
    OTPTxtFields.first.becomeFirstResponder()
}

第 2 步:

在你当前页面的 UITextField 委托方法

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 以访问向后功能

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 的回答为您的 UITextFieldclass 定制

步骤 5

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

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

}

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