анализ типа UITextField для функции @ obj c во время действий - PullRequest
0 голосов
/ 27 марта 2020

Итак, я хочу добавить кнопку «Готово» в мой decimalPad, чтобы пользователь мог прекратить редактирование. но мне нужно проанализировать UITextField для функции handleInput при нажатии кнопки готово. у кого-нибудь есть идеи, могу ли я разобрать UITextField для целевой функции C или есть лучший способ сделать это?

когда я делаю это так, я получаю эту ошибку:

Аргумент «#selector» не относится к методу, свойству или инициализатору «@ obj c»

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)
}

1 Ответ

0 голосов
/ 28 марта 2020

В итоге я создал новую функцию, которая проходит через один из стековых представлений и ищет отредактированное поле 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()
}
...