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 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.
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!
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With