0

So I want to add done button to my decimalPad so that the user can stop editing. but I need to parse the UITextField to the handleInput function when the done button is pressed. anyone have any ideas if I can parse a UITextField to a objective C function or of there is a better way to do this?

when im doing it like this I get this error:

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)
}
Petter Braka
  • 311
  • 1
  • 11

1 Answers1

-1

I ended up making a new function that goes through one of the stackViews and searches for a UITextField that has been edited.

I have attached the code in case anyone else has the same issue.

/**
 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()
}
Petter Braka
  • 311
  • 1
  • 11