If I want to handle changes to a UITextField, such as the user typing in it; it seems like this can be done either by assigning a delegate to that text field, and then having th
shouldChangeCharactersInRange
is called before a change occurs, and gives you opportunity to 'cancel' the change. UIControlEventEditingChanged
is called after the change occurred.
You can determine the resulting value of the textField in shouldChangeCharactersInRange
, but you have to manually apply the replacementString to the existing text, using the supplied range. (via NSString stringByReplacingCharactersInRange
). If you want to know the resulting text, it's easier and more efficient to use UIControlEventEditingChanged
.
shouldChangeCharactersInRange
is often used to implement validation checking of input - that is, you can filter characters/pasted text as it is entered. If a field is for phone numbers, for example, you can return FALSE
if the user types a non numeric character, or attempts to paste in text that isn't numeric.
You might find a case where you can reuse code for multiple controls if you can stick with the UIControlEvent-methods.