简体   繁体   中英

How to determine which textfield is active swift

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 a view with a UIPickerView , UIToolBar and two bar button items. The cancel item has an action that will clear its text only i cant figure out how to get the correct UITextField .

I've tried these both:

@IBAction func cancelText(sender: AnyObject) {


    if self.truckYear.editing == true{

        println("truckYear")

        clearText(self.truckYear)
    } else if self.truckMake.editing == true{

        clearText(self.truckMake)
    }
}
@IBAction func doneText(sender: AnyObject) {

    pickerView.hidden = true
}


func clearText(textField:UITextField){

    println("in clear text")

    textField.text = nil
}

And:

@IBAction func cancelText(sender: AnyObject) {


    if self.truckYear.isFirstResponder(){

        println("truckYear")

        clearText(self.truckYear)
    } else if self.truckMake.isFirstResponder(){

        clearText(self.truckMake)
    }
}
@IBAction func doneText(sender: AnyObject) {

    pickerView.hidden = true
}


func clearText(textField:UITextField){

    println("in clear text")

    textField.text = nil
}

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

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.

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

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!

你可以为你的所有文本字段分配一个标签,并在textFieldDidBeginEditing中检查你当前的textFeild.tag,并且很明显巫婆textfeild正在使用中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM