(I'm not saying it isn't, this is more of me asking in order to make sure I'm properly implementing object-oriented principles.)
Say I want to create a UIView
with a UIImageView
in it and a UILabel
below the UIImageView
captioning it.
In an iOS app, what would be the best way to reuse this UIView
setup if I wanted to have it in multiple parts of my app?
It seems a lot of tutorials I read indicate to subclass UIView
, and in one of the init
methods create the subviews and then add them to self
, and voila, just create new instances of this UIView
subclass and you have a reusable
implementation.
But is this "image with caption" concept make sense to be a subclass of UIView? I understand subclassing and inheritance to be something like you have a superclass called Vehicle
, and you create a subclass of it called Truck
. If you look at it, "Truck" is a "Vehicle" which is how I've understood inheritance.
Does
"image with caption" is a "view"
make conceptual sense in inheritance? I know many would say "Of course it is!" but I'm just looking at it and thinking that it's not exactly a derivation or a customization of the superclass, but an amalgamation of some other ones.
If I'm creating something like that, I'm almost thinking I want a reusable customized instance of UIView, more so than a subclass of UIView but maybe I'm applying too sharp a definition of subclassing.
What if I just wanted a bunch of red UIButtons
with a special font for the label that I could reuse? Is that really a use for subclassing?
Is subclassing the best option? Or maybe creating it from a nib and importing it with code would be better?