Search code examples
iosobjective-csubclassclass-methodobjective-c-category

iOS: Creating a method that can be used by different objects?


I'm trying to create a method that can be called from any object that has a property backgroundColor. Should I subclass NSObject and add a class method or is there a different way to do something like this, that doesn't require adding a category for every object.

Something along the lines of:

[self.myLabel addAwesomeLayerWithSomeColor:(UIColor *)color];
[self.myView addAwesomeLayerWithSomeColor:(UIColor *)color];
[self.myButton addAwesomeLayerWithSomeColor:(UIColor *)color];

Solution

  • Looks like you want this method to be usable on views and subclasses of views. The correct thing to do would be to make a category on UIView. Then, all of these subclasses and any others will be able to use this method.