我是个敏捷的初学者。我在ViewController中实现了此功能,但是我不想在每个ViewController中都编写这样的代码。我想通过协议来实现它,但是出了点问题。
import UIKit
@objc protocol TextFieldAutoHiddenKeyboard: class {
var textFieldAutoHidenLenth: UInt {get set}
}
extension TextFieldAutoHiddenKeyboard where Self: UIViewController {
func autoHiddenKeyboardWhenFillUpTextFiled(textField textF: UITextField, autoHidenLenth: UInt) {
textFieldAutoHidenLenth = autoHidenLenth
let textFieldDidChangeActionName = "textFieldDidChange(textField:)"
let textFieldDidChangeAction = Selector(textFieldDidChangeActionName)
textF.addTarget(self, action: textFieldDidChangeAction, for: .editingChanged)
let dismissKeyboardActionName = "dismissKeyboard"
let dismissKeyboardAction = Selector(dismissKeyboardActionName)
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: dismissKeyboardAction)
view.addGestureRecognizer(tap)
//textF.addTarget(self, action: #selector(textFieldDidChange(textField:autoHidenLenth:)), for: .editingChanged)
}
func textFieldDidChange(textField: UITextField) {
if let text = textField.text {
if text.count == textFieldAutoHidenLenth {
textField.resignFirstResponder()
}
}
}
func dismissKeyboard() {
view.endEditing(true)
}
//@objc func textFieldDidChange(textField: UITextField, autoHidenLenth: UInt) {
// if let text = textField.text {
// if text.count == autoHidenLenth {
// textField.resignFirstResponder()
// }
// }
//}
}
#selector(textFieldDidChange(textField:autoHidenLenth:))
创建动作,我不知道如何将第二个参数传递给函数。例如,参数autoHidenLenth。答案 0 :(得分:0)
您必须为此实现一个委托方法:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let char = string.cString(using: String.Encoding.utf8)!
let isBackSpace = strcmp(char, "\\b")
if (isBackSpace == -92) {
print("Backspace was pressed")
return true
}
if((textField.text?.length)! == 6){
self.view.endEditing(true)
return true
}
return true
}
并且不要忘记分配代理人
textfield.delegate = self
答案 1 :(得分:0)
您使用了错误的文本字段委托。您应该使用textfield的代表,该代表对字符的更改而不是对字段的更改进行响应。
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let length = (yourtextfield.text?.characters.count)! - range.length + string.characters.count
if length == 6 {
yourtextfield.resignFirstResponder()
return true
}else {
return true
}
}
答案 2 :(得分:0)
您不应使用字符串文字构造选择器,而应使用#selector(textFieldDidChange(textField:))
,然后您会发现选择器方法需要公开给ObjectiveC。
因此,您应该可以通过以下方式修复它:
@objc func textFieldDidChange(textField: UITextField) {
if let text = textField.text {
if text.count == textFieldAutoHidenLenth {
textField.resignFirstResponder()
}
}
}
答案 3 :(得分:0)
由于您需要为多个文本字段启用此功能,因此可以使用它。
private var autoHiddenLengthAssociationKey: UInt8 = 0
extension UITextField {
private var autoHiddenLenth: Int? {
get {
return objc_getAssociatedObject(self, &autoHiddenLengthAssociationKey) as? Int
}
set {
objc_setAssociatedObject(self, &autoHiddenLengthAssociationKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
}
}
func enableAutoHidingFeature(maxLength: Int) {
autoHiddenLenth = maxLength
self.addTarget(self, action: #selector(textDidChange(textField:)), for: .editingChanged)
}
@objc func textDidChange(textField: UITextField) {
guard let maxLength = autoHiddenLenth, let text = textField.text else { return }
if text.count == maxLength {
textField.resignFirstResponder()
}
}
}
您可以通过以下方式启用此功能:
textField.enableAutoHidingFeature(maxLength: 6)