iphone - 即使 UITextField 为空,我也能检测到删除键吗?

标签 iphone ios uitextfield

当UITextField不包含任何内容时,按键盘删除键不会调用任何UITextFieldDelegate的方法。

我怎样才能检测到它?

编辑: 似乎没有简单的方法可以做到这一点。我能找到的最有用的链接是:

  1. UITextField : Any way to detect the Delete key event when the field is empty ?

  2. How to get the actual key pressed in a UITextField

简而言之,我的解决方案是在文本字段的开头放置一个永久的 SPACE。并进行其他必要的更改(textFieldShouldReturn:、textField:shouldChangeCharactersInRange:replacementString: 等)。

最佳答案

我正在为 swift 3 中的更完整示例添加此答案。 基本上,我需要一个 pincode 类型的 View ,其中我有多个文本字段,每个单元格中允许一个字符。

像这样 enter image description here 我首先创建了 UITextField 的子类和定义新函数的协议(protocol)。

protocol MYDeleteActionTextFieldDelegate {
    func textFieldDidSelectDeleteButton(_ textField: UITextField) -> Void
}
class MYDeleteActionTextField: UITextField {
    var deleteDelegate: MYDeleteActionTextFieldDelegate?
    override func deleteBackward() {
        // Need to call this before super or the textfield edit may already be in place
        self.deleteDelegate?.textFieldDidSelectDeleteButton(self)
        super.deleteBackward()
    }
}

然后您使用新的子类创建文本字段并在您的 View Controller 中实现委托(delegate)。就我而言,我管理数组中的文本字段以便于使用,并使用 PureLayout 对单元格进行布局。我这样存储它们

var pinFields = UITextField

然后在 viewDidLoad() 中,我将所有的 pin 字段添加到数组中,如下所示:

for _ in 1...6 {
            let field = EDFDeleteActionTextField.init(forAutoLayout: ())
            field.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
            field.delegate = self
            field.deleteDelegate = self
            field.textAlignment = .center
            field.font = UIFont.newBigTitle()
            field.textColor = UIColor.edfBlue()
            field.backgroundColor = UIColor.edfWhite()
            self.pinFields.append(field)
            self.pinView.addSubview(field)
        }

现在您只需要响应所有适当的委托(delegate)方法和上面添加的 textFieldDidChange 目标。

// MARK: UITextFieldDelegate
    func textFieldDidChange(textField: UITextField) {
        // If the user typed one character, move to the next cell.
        if (textField.text?.characters.count == 1) {
            let index = pinFields.index(of: textField)
            textField.resignFirstResponder()
            if (pinFields.count > index! + 1) {
                pinFields[index! + 1].becomeFirstResponder()
            }
        } // If they deleted the character move to previous cell
        else if (textField.text?.characters.count == 0) {
            let index = pinFields.index(of: textField)
            if (index! - 1 >= 0) {
                pinFields[index! - 1].becomeFirstResponder()
            }
        }
    }

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if range.location > 0 {
            let index = pinFields.index(of: textField)
            // If there is already text in the text field and the next cell is empty - move the newly typed character to that cell.
            if (pinFields.count > index! + 1) {
                let nextField = pinFields[index! + 1]
                if (nextField.text?.characters.count == 0) {
                    textField.resignFirstResponder()
                    nextField.becomeFirstResponder()
                    nextField.text = string
                }
            }
            return false
        }
        return true
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return false
    }

    // MARK: EDFDeleteActionTextFieldDelegate
    func textFieldDidSelectDeleteButton(_ textField: UITextField) {
        // If user clicked delete, and there are no characters, move to previous cell if available.
        // If there are characters, it is handled in UITextFieldDelegate
        if (textField.text?.characters.count == 0) {
            let index = pinFields.index(of: textField)
            if (index! - 1 >= 0) {
                pinFields[index! - 1].becomeFirstResponder()
            }
            else {
                textField.resignFirstResponder()
            }
        }
    }

我将省略无聊的部分(比如布局文本字段等),因为这个通用功能在更多情况下比这个 pincode View 更有用,但是实现这个子类和协议(protocol)应该提供你需要的所有功能对于类似类型的 View 并解决手头的问题(可能需要类似的东西)。

快乐编码。

关于iphone - 即使 UITextField 为空,我也能检测到删除键吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5958574/

相关文章:

ios - Xcode 6 LaunchScreen.xib 错误的 iOS7 iPhone 5 窗口大小

iphone - 如何正确使用 nsoperationqueue 的 autoreleasepool

html - 我的响应式 HTML 电子邮件没有响应

iphone - 如何在 UIDatePickerModeDate 中显示 UIDatePicker 的星期几?

iphone - 从 .NET WCF4 Web 服务返回 .plist

ios - 检查 Array 中的 Int 。 swift 3,iOS

ios - Pod 安装未在 Flutter 应用程序中安装 Pod

ios - iOS 11.3 中状态栏背景颜色问题

ios - UIPickerView选定的RowInComponent :0 always return zero first when I try to set data in second component

ios - 如何验证文本字段