Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine which textfield is active swift

Tags:

ios

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 } 
like image 238
Shawjak3 Avatar asked Jun 18 '15 14:06

Shawjak3


People also ask

How do I know if my TextField is a first responder?

You could keep track of which text field is the first responder by either setting your view controller to be the delegate object of all text fields and then when your subclassed text fields gets the " becomeFirstResponder " method call, tell your view controller which text field is the current one.

How check TextField is empty or not in Swift?

storyboard add one textField, one button and one label one below other as shown in the figure. On click of the button we will check whether the text field is empty or not and show the result in label. @IBOutlet weak var textField: UITextField! @IBOutlet weak var resultLabel: UILabel!


2 Answers

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")    } } 
like image 114
Randy Avatar answered Sep 22 '22 06:09

Randy


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() 
like image 20
Reimond Hill Avatar answered Sep 23 '22 06:09

Reimond Hill