Check for exiting TextField resignedFirstReponder?

938 Views Asked by At

Is it possible to check if a TextField has been resigned?

I have 3 TextFields, and Cycle through them with return key with (resignFirstResponder) and (becomeFirstResponder)

But is there a way to check if a field has resigned? I noticed some of my app testers don’t use return key but click manually on the fields, and that way my previous field doesn’t save data the way it should.

What’s the best way to check if a user clicked away from a TextField ? Either on to a next text field or temporary away ?

1

There are 1 best solutions below

2
Jawad Ali On BEST ANSWER

isFirstResponder Apple Docs

Returns a Boolean value indicating whether this object is the first responder.

var isFirstResponder: Bool { get }

how to use

if txtField.isFirstResponder {
// do whatever you want to
}

If you want to save data when textField change focus .. you should implement delegate method of text field

Before resigning as first responder, the text field calls its delegate’s textFieldShouldEndEditing(_:) method. Use that method to validate the current text.

func textFieldShouldEndEditing(_ textField: UITextField) -> Bool

in your case yo can check which texfield ends editing

func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
   if textField == textfield1 {
     // save 
   }
}

Apple Docs for textFieldShouldEndEditing