0

Beginner here, hopefully a simple question about preventing code duplication.

If you have code that detects what is being typed in a UITextView (ie. regex for validation purposes), and you want that code to apply to multiple UITextView's on different screens, how do you go about doing that rather than just duplicating that code in each and every UIViewController that utilizes that UITextView?

i.e.

-(BOOL)textField:(UITextField*)textFieldshouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
Kampai
  • 22,848
  • 21
  • 95
  • 95
SimplyLearning
  • 269
  • 3
  • 11
  • Have a validation class method on some utility object. – i_am_jorf Nov 03 '14 at 16:59
  • Have a shared delegate? – Ian MacDonald Nov 03 '14 at 17:01
  • @IanMacDonald, are you referring to the AppDelegate? – SimplyLearning Nov 03 '14 at 18:15
  • No, I'm referring to your `UITextField`'s delegate. If you assign the same object as a delegate to multiple `UITextField` objects, you will accomplish what you're requesting. – Ian MacDonald Nov 03 '14 at 18:16
  • @jeffamaphone, do you mean something like this: http://stackoverflow.com/questions/1658433/accessing-method-from-other-classes-objective-c and http://stackoverflow.com/questions/11479638/ios-how-do-i-create-a-utility-class-that-all-my-controllers-can-call Essentially I would just create a helper method that I call inside each of my textFieldshouldChangeCharactersInRange methods right? That way I just import from the util each time, and call one line instead of having the same code in all my classes? – SimplyLearning Nov 03 '14 at 18:18
  • Something like that, yes. You could also make a custom UIViewController category, for example. There are a few different ways to accomplish this. – i_am_jorf Nov 03 '14 at 18:24
  • @IanMacDonald, not really sure I'm following...from what it sounds like, you're suggesting a way to make sure multiple `UITextField` objects all have the same validation based off of the delegates correct? If so, I don't think that would help since I'm looking for a way to use those same delegates on different `UIViewControllers` without having to copy/paste hundreds of lines of code. Unless I'm missing something here? – SimplyLearning Nov 03 '14 at 18:27

1 Answers1

0

You could make a class specifically for this purpose. If you need to do special UITextFieldDelegate stuff, you can either subclass this class or attach your own delegate to it.

@interface TextValidationDelegate : NSObject <UITextFieldDelegate>
@property (weak) id<UITextFieldDelegate> delegate;
@end

@implementation TextValidationDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  BOOL acceptable = YES;
  // TODO: Calculate acceptability.
  // ...

  if (acceptable && [self.delegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
    acceptable = [self.delegate textField:textField shouldChangeCharactersInRange:range replacementString:string];
  }

  return acceptable;
}

// TODO: Override other UITextFieldDelegate methods and pass through to self.delegate when it respondsToSelector.
@end
Ian MacDonald
  • 13,472
  • 2
  • 30
  • 51