How to override a delegate function of uitextfield such that it does the validation of entered text. ie i need to override the textfield did end editing开发者_开发知识库 method to perform validation and this should apply to all textfield in my project.can i do this using categories?
Validation should be handled in textFieldShouldEndEditing:
, not textFieldDidEndEditing:
. "should" methods are called before something finishes to allow validation. "did" methods are notifications of something that has already happened.
EDIT You would not use a category on UITextField
for this. There are lots of good solutions.
- Have a single, shared
UITextFieldDelegate
for all of your text fields - Subclass your delegates from a consistent superclass that implements this the way you want
- Put the shared logic into a function that your delegates call in order to avoid subclassing
- Put your shared logic into a singleton
TextFieldController
that all your text field delegates would reference.
It all depends on the kind of logic you need in this routine. But you shouldn't use categories to override existing methods.
You can put a general validation by using categories for textfield.
Again, as Rob says, it all depends on the logic your application needs.
精彩评论