在操作期间将 UITextField 类型解析为 @objc 函数

parse of type UITextField to an @objc function during actions

所以我想在我的 decimalPad 中添加完成按钮,以便用户可以停止编辑。但是当按下完成按钮时,我需要将 UITextField 解析为 handleInput 函数。如果我可以将 UITextField 解析为 objective C 函数或有更好的方法来执行此操作,有人有任何想法吗?

当我这样做时,我得到了这个错误:

Argument of '#selector' does not refer to an '@objc' method, property, or initializer

let userInput = UITextField(frame : CGRect(x: 0, y: 0, width: 200, height: 40))
userInput.attributedPlaceholder = NSAttributedString(string: "0.000", attributes: [NSAttributedString.Key.foregroundColor: UIColor.lightGray])
userInput.textColor = UIColor.white
userInput.textAlignment = .right
userInput.tag = units.firstIndex(of: unit)!
userInput.font = UIFont(name: "American Typewriter", size: 20)
userInput.borderStyle = UITextField.BorderStyle.roundedRect
userInput.keyboardType = UIKeyboardType.decimalPad
userInput.returnKeyType = UIReturnKeyType.done
userInput.backgroundColor = UIColor.darkGray
userInput.delegate = self

let toolBar = UIToolbar()
toolBar.sizeToFit()
let flexibleSpaced = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneClicked(userInput)))

@objc func doneClicked(_ textField: UITextField){
    view.endEditing(true)
    handleInput(textField)
}

我最终创建了一个新函数,该函数遍历其中一个 stackView 并搜索已编辑的 UITextField。

我已附上代码,以防其他人遇到同样的问题。

/**
 This function will go through each one of the infocards in the infoStack. it will not stopp before it finds a textfield that has something writen in it.

 - returns: **searching** it contains the UITextField that has been edited.

 # Example #
 ```
 textField = searchForUpdates()
 ```
 */
func searchForUpdates() -> UITextField {
    for infocard in infoStack.subviews {
        for view in infocard.subviews {
            if view is UITextField, let searching = view as? UITextField {
                if searching.text != "" {
                    return searching
                }
            }
        }
    }
    return UITextField.init()
}