views:

34

answers:

1

I have a UITextFieldDelegate that does a whole bunch of validation on user input to determine whether or not they should be allowed to end editing. In one particular example, it is not valid to leave the field blank.

Right now I'm using textField:shouldChangeCharactersInRange:replacementString: to validate the text input after each edit by the user.

The problem is this: if the user clears the field (with the little 'x' button), the validation code goes into "invalid" mode and prevents the user from navigating away until they have entered valid text. If the user then shakes the phone to get the old text back, shouldChangeCharactersInRange is not called again and the delegate stays in the "invalid" state instead of recognizing that everything is fine again.

Not sure if I'm using it correctly, but it seems like the built-in UITextFieldDelegate machinery is not able to cope with text changes due to undo / redo.

What's the best way to achieve proper validation in this scenario? Do I really need to subclass UITextField in order to implement motionEnded:withEvent:? Seems like the edit-handling stuff in UITextField should really be independent of whether the user actually typed it or it happened due to undo, so would be bummed if I actually had to go that route.

+3  A: 

Hook up a method to the UIControlEventEditingChanged event ("Editing Changed" in IB -- not "Value Changed").

This appears to fire whenever/however the text field changes.

aBitObvious
Excellent, thanks. Works a treat and is so much simpler than implementing `shouldChangeCharactersInRange` and `textFieldShouldClear` which is what I was doing before. Sigh of relief!
glenc