ios - Share validation code among different UITextFields -
maybe it's simple question can't figure out right way it.
i have uitextfield
. since need validate text inserted within text field, i'm using uitextfielddelegate
method - (bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string
following:
- (bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string { // validation here // return yes or no depending on previous validation }
since have text fields (they not belong same controller validation performed) , don't want duplicate text fields validation code, how can achieve elegant way centralize similar validation? don't know if subclassing right way, in case need use same delegation method (the 1 listed above) in different parts of same app.
thank in advance.
this depends on how information validation code requires. if arguments of textfield:shouldchangecharactersinrange:replacementstring:
need determine whether return yes or no, can use same delegate textfields.
if need track , store information previous validations in order make validation decisions, easiest way package state object, , have array of objects, each corresponding specific uitextfield. then, in textfield:shouldchangecharactersinrange:replacementstring:
use state corresponding textfield passed in.
Comments
Post a Comment