Search code examples
iosobjective-cdelegatesuitextfielduitextfielddelegate

Can a static class be set as delegate for a UItextfield (Objective C)?


In my current project I have a few viewcontrollers that use a UItextfield. As of now I have all the viewcontrollers set as delegates for the textField. However, I have all the same code for each of the viewcontrollers (at least for the UItextfield delegate methods). Is there a way I can just create a static class and set that as the delegate for all the UItextfields?


Solution

  • You can achieve using two ways.

    1. Once create a singleton class that can handle your delegates and assign the singleton instance as your delegate.
    2. You can inherit the UITextField say myTextField and implement all the delegate methods which you want to override. For all the instances where you want to use TextField and have the custom behavior use myTextField.