Search code examples
iosobjective-cinheritanceuiviewsubclassing

Why do you subclass UIView?


So basically i have been looking of a way to write my code which has alot of uiview instances(makes everything look so messy ! erk!) more readable and elegant.

So far i have seen the use of subclassing but don't really understand the importance/reason of using the practice !

From what i understand:

"class2.m":
  1. You make a class which inherits from the UIView.

    "class1.m":

  2. From the superclass "class1" you make an instance of class one which inherits from class2 ?

So what i am trying to understand is that why would you have a class which inherits from a class2.m that inherits from another (UIView), instead of inheriting UIView directly from class1 ?

Sorry i sound like such a stubborn person but i simply just don't get it ? I really want to know why...

Regards

Edit

Can someone please explain using an example of something like a class called userProfile and guestProfile that subclasses Profile ?


Solution

  • An analogy is that say a UIView is like an Animal, now class1.m is a bird and class2.m is a duck. a duck is a bird which is an animal (inheritance)

    now if we were to make a new bird, we dont want to just inherit from animal otherwise we will need to recode everything bird defines, which just adds code bloat and wastes time.

    but say you want to make a dog, we cant obviously inherit from bird, because a dog doesnt have any of the features of a bird, so we would have to inherit from animal instead.

    hope that clarifies things a bit