How to determine which textfield is active swift
You can declare a UITextField
property in your class, and assign the current text field to it in textFieldDidBeginEditing
.Then you can just call this text field whenever you need to.
class ViewController : UIViewController, UITextFieldDelegate { var activeTextField = UITextField() // Assign the newly active text field to your activeTextField variable func textFieldDidBeginEditing(textField: UITextField) { self.activeTextField = textField } // Call activeTextField whenever you need to func anotherMethod() { // self.activeTextField.text is an optional, we safely unwrap it here if let activeTextFieldText = self.activeTextField.text { print("Active text field's text: \(activeTextFieldText)") return; } print("Active text field is empty") }}
In swift 4:
you can get the active UITextField like that
extension UIView {func getSelectedTextField() -> UITextField? { let totalTextFields = getTextFieldsInView(view: self) for textField in totalTextFields{ if textField.isFirstResponder{ return textField } } return nil}func getTextFieldsInView(view: UIView) -> [UITextField] { var totalTextFields = [UITextField]() for subview in view.subviews as [UIView] { if let textField = subview as? UITextField { totalTextFields += [textField] } else { totalTextFields += getTextFieldsInView(view: subview) } } return totalTextFields}}
and then you call it like that
let current = view.getSelectedTextField()
In order to improve @Reimond's answer Swift 4
extension UIView { var textFieldsInView: [UITextField] { return subviews .filter ({ !($0 is UITextField) }) .reduce (( subviews.compactMap { $0 as? UITextField }), { summ, current in return summ + current.textFieldsInView }) } var selectedTextField: UITextField? { return textFieldsInView.filter { $0.isFirstResponder }.first }}
usage:
view.selectedTextField