文本字段退格操作识别文本字段何时为空? [英] textfield backspace action identify when textfield is empty?

查看:25
本文介绍了文本字段退格操作识别文本字段何时为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我试过了:-

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) ->布尔{让 char = string.cStringUsingEncoding(NSUTF8StringEncoding)!让 isBackSpace = strcmp(char, "\\b")如果(isBackSpace == -92){println("退格被按下了")}返回真}

但是当文本字段不为空时会调用它.

例如:-

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

谢谢

解决方案

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

步骤 5

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

func callOTPValidate(){var 文本:[字符串] = []OTPTxtFields.forEach { texts.append($0.text!)}sentOTPOption(currentText: texts.reduce("", +))}func sentOTPOption(currentText: String) {print("AllTextfieldValue == \(currentText)")}

I am trying to create otp textfield using five textfield.All working fine if you add top, but issue is occurred when user try to add textfield empty and trying to backspace and it was not call any delegate method of UItextfiled which I already added.

I tried this :-

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
}

but it's called when textfield is not empty.

For example :-

In below screen shot add 1 and on two different textfield and third one is empty but when I try to backspace it's need to go in second textfield(third is field is empty) this is what I was facing issue from mine side.

Thanks

解决方案

followed by @Marmik Shah and @Prashant Tukadiya answer here I add my answer , for quick answer I taken the some code from here

step 1 :

create the IBOutletCollection for your all textfields as well as don't forget to set the tag in all textfields in the sequence order, for e.g [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()
}

step 2 :

in your current page UITextField delegate method

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()
    }
}

}

Step 3:

create the textfield class for access the backward function

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)
}

step - 4

finally follow the @Marmik Shah answer for custom class for your UITextField

Step 5

get the values from each textfield use this

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

}

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

这篇关于文本字段退格操作识别文本字段何时为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆