I cant figure out which UITextField is currently active so i can clear its text if they hit cancel on a UIBarButtonItem. Here is my code. There is
/// Finds the textField that is the first responder in a view hierarchy
func findActiveTextField(in subviews: [UIView], textField: inout UITextField?) {
guard textField == nil else { return }
for view in subviews {
if let tf = view as? UITextField, view.isFirstResponder {
textField = tf
break
}
else if !view.subviews.isEmpty {
findActiveTextField(in: view.subviews, textField: &textField)
}
}
}
// Usage
var aView: UIView = UIView()
var activeField : UITextField?
findActiveTextField(in: aView.subviews, textField: &activeField)