How to determine which textfield is active swift

前端 未结 7 1716
太阳男子
太阳男子 2020-12-06 04:01

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

相关标签:
7条回答
  • 2020-12-06 04:42

    Use the delegate:

    optional func textFieldDidEndEditing(_ textField: UITextField)
    

    This will tell you when the active textfield finishes editing, and textField will contain a pointer to the one that's no longer going to be active.

    0 讨论(0)
  • 2020-12-06 04:45

    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")
       }
    }
    
    0 讨论(0)
  • 2020-12-06 04:46

    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()
    
    0 讨论(0)
  • 2020-12-06 04:51

    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

    0 讨论(0)
  • 2020-12-06 04:59
    /// 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)
    
    0 讨论(0)
  • 2020-12-06 04:59

    I wrote a library to handle basic Form Creation Swift Pop Form. It doesn't use tags but rather sets a datasource that contains fields and uses indexPathForCell to query which is the active textfield. Check out the source code for more!

    0 讨论(0)
提交回复
热议问题