Swift UITextField deleteBackward问题

时间:2016-07-29 10:07:48

标签: ios swift algorithm uitextfield

我在一行中有一系列textfields。每个textfield限制为两个字符。

我的问题是我要删除所有字符并返回所有textfields,直到我到达第一个textfield

我已经对textfield进行了子类化并覆盖了deleteBackward方法,以确定textfield中的文本是否为空。如果是,则将响应者更改为上一个文本字段。

这一切都很好,但是当文本字段中有一个字符并按下退格键时,该字符将被删除,响应者将更改为上一个文本字段。

这是不正确的行为,因为我希望焦点停留在已删除其字符的文本字段上,如果再次按下删除键,则响应者应更改为上一个文本字段。

我在textfield的shouldChangeCharacters委托方法中更正了我的设置标志。如果...

,该标志设置为true
if (newLength == 0 && currentLength == 0) || currentLength == 0 {
  textFieldEmpty = true
} else {
  textFieldEmpty = false
}

这样可以正常工作,但是当textfield为空并按下退格键时,不会调用此委托方法?

如果有人解决了类似的问题以及他们采取了什么方法,那就徘徊吧?

由于

1 个答案:

答案 0 :(得分:1)

我有一个类似的场景,但在每个文本字段中都有单个字符。我还在自定义textField中使用了deleteBackward来实现这一点。

试试这段代码:

import UIKit
protocol CustomTextFieldDelegate{
 func textFieldDidDelete()
}
class CustomTextField: UITextField {
  var myCustomTextFieldDelegate : CustomTextFieldDelegate!
  override func deleteBackward() {
     super.deleteBackward()
     myCustomTextFieldDelegate.textFieldDidDelete()
 }
}

这就是你应该如何实现deleteBackward方法:

func textFieldDidDelete() {
    print("Entered Delete");
    print("Tag!!\(currentActiveTextField.tag)")
    var previousTag = 0
    if currentActiveTextField.text == ""{
      previousTag = currentActiveTextField.tag - 1
    }else{
     previousTag = currentActiveTextField.tag
    }
    let previousResponder = currentActiveTextField.superview?.viewWithTag(previousTag)
    if(previousTag > 0){
        let previousTextField = previousResponder as! CustomTextField
        previousTextField.text = ""
        currentActiveTextField = previousTextField
        previousResponder?.becomeFirstResponder()
    }
}

同样在range方法中的shouldChangeCharacters中,不要忘记将currentActiveTextField设置为您要编辑的文本字段。检查以下代码,可能会有所帮助。我的文本字段有标签1,2,3,4。

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    currentActiveTextField = textField as! CustomTextField
    let nextTag = textField.tag + 1;
    // get next responder
    var nextResponder = textField.superview?.viewWithTag(nextTag);


    // On inputing value to textfield
    if (textField.text?.characters.count < 1  && string.characters.count > 0){

        if (nextResponder == nil){
            nextResponder = textField.superview?.viewWithTag(1);
        }
        textField.text = string;

        if nextTag == 5 {
            self.digitFourTextField.resignFirstResponder()
            return true;
        }

        nextResponder?.becomeFirstResponder();
        return false;
    }
    if (textField.text?.characters.count >= 1  && string.characters.count == 0){
        // on deleteing value from Textfield
        let presentTag = textField.tag;
        // get next responder
        var presentResponder = textField.superview?.viewWithTag(presentTag);

        if (presentResponder == nil){
            presentResponder = textField.superview?.viewWithTag(1);
        }
        textField.text = "";
        presentResponder?.becomeFirstResponder();
        return false;
    }

    let currentCharacterCount = textField.text?.characters.count ?? 0
    let newLength = currentCharacterCount + string.characters.count - range.length
    if newLength > 1 {
        textField.text = string
        if (nextResponder != nil){
            nextResponder?.becomeFirstResponder();
        }

    }
    let returnVal = newLength <= 1
    return returnVal;
}