Shawjak3
Shawjak3

Reputation: 541

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
}

Upvotes: 34

Views: 48594

Answers (7)

Hoorad Ramezani
Hoorad Ramezani

Reputation: 36

you can assign a tag to all your text feild and in textFieldDidBeginEditing check your current textFeild.tag and it's clear witch textfeild is in use .

Upvotes: 1

Vyacheslav
Vyacheslav

Reputation: 27221

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

Upvotes: 14

aBikis
aBikis

Reputation: 341

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!

Upvotes: 0

Reimond Hill
Reimond Hill

Reputation: 4760

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

Upvotes: 24

Randy
Randy

Reputation: 4535

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

Upvotes: 56

John Slade
John Slade

Reputation: 219

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

Upvotes: 0

Almo
Almo

Reputation: 15871

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.

Upvotes: 0

Related Questions