Select baseclass type at runtime like templates

117 Views Asked by At

I have a component which is used as the base class for a lot of more complex components.
Currently this base inherits from UIScrollView.

What I want now, is to have an alternative base view with 99% of the same functionality, but which will inherit from UIView. In C++ I would most probably have done this with templates, but how can this be done in Objective-C?

Example:

                |-- ComplexDerivedClass_1
                |
BaseClass  -----|-- ComplexDerivedClass_2    |-- EvenMoreComplexDerived_1
                |                            |
                |-- ComplexDerivedClass_3 ---|-- EvenMoreComplexDerived_2 
                                             |
                                             |-- EvenMoreComplexDerived_3

My aim is that BaseClass may inherit either from UIView or from UIScrollView and that this will be decided at runtime (without creating special implementations of each derived class for either UIView type base or UIScrollView type base).

One solution is of course to have the base class inherit from UIView, and if required, add another UIScrollView to it at runtime, but I am wondering whether it can be done without this.

1

There are 1 best solutions below

0
On

Have you looked at protocols? I'm not that good with c++, but I think that's the closest thing.