Detecting if the user is typing?

2k views Asked by At

For a UITextField, what is the best way to detect if the user is typing and the textfield has a value?

I've tried the following unsuccessfully:

  • Value Changed: no response
  • Editing Did End: no response
  • Touch Up Inside: doesn't trigger until after the user clicks out of the textfield
1

There are 1 answers

0
MLyck On

Try using the shouldChangeCharactersInRange event.

func textField(textField: UITextField, 
           shouldChangeCharactersInRange range: NSRange, 
           replacementString string: String) 
 -> Bool {
  // Put your code here
}

This is called whenever a user adds or removes a new character to your UITextField.

If you want to accept the change, return true. Otherwise return false.

Just make sure your UITextField conforms to the UITextFieldDelegate Protocol