Better subclass UIElements or create Category to customize

97 views Asked by At

I want to customize many UIElements easily, so I have read that I can do it by Category the UILabel, ex. UILabel+Custom, or subclassing the UIElement. I just want to know when is better to use Category and when is better Subclassing. Or maybe one of those is the best.

1

There are 1 answers

1
Christian Di Lorenzo On

As @rmaddy mentioned in a comment, it depends what sorts of things you are trying to do. I'll try to cover a few cases for you to answer your question.

Subclassing — should be used when you need to...

  • override an object's methods
  • defining a more specific object (e.g. a Truck should subclass the more generic object Car)

Categories — should be used when you need to...

  • provide additional functionality for all types of the generic class
  • access methods in a system class or change an existing method's functionality where the class you don't directly instantiate that system object (NOTE: should be used only in rare cases)

If there are any other suggestions for when to use subclassing versus categories, feel free to add them in the comments.

@RoxeeMan, does this seem to answer your question?