I have a class named SPPanelManager
, which has a property of another class, named SPPanelSettingsManager
. SPPanelManager has the following in it's -init
method:
self.settingsManager = [[SPPanelSettingsManager alloc] init];
The purpose of SPPanelManager
is to be subclassed, and the subclasses are used throughout my app. For example, there's SPGreetingManager
. In the .h file of SPGreetingManager
, I have declared:
@property (nonatomic, strong) SPGreetingSettingsManager *settingsManager;
which makes the settingsManager be of the correct class. The problem is that when the SPGreetingManager
subclass is initialized, it calls the init method above, and initializes the settingsManager as the SPPanelSettingsManager
class, rather than SPGreetingSettingsManager
.
How can I get it to initialize this as the correct class for that property without having to re-write the init code in every subclass?
The super class (SPPanelManager
) somehow has to know which class the concrete panel manager wants to use as a settingsManager
.
Apple uses the following approach to match CALayers to UIViews:
The base class declares a class method that returns the concrete SPPanelSettingsManager
subclass:
// in SPPanelManager.h
+ (Class)settingsManagerClass;
... which subclasses override to return their custom class:
// in SPGreetingManager.m
+ (Class)settingsManagerClass
{
return [SPGreetingSettingsManager class];
}
Now the superclass can instantiate the settings manager as follows:
self.settingsManager = [[[[self class] settingsManagerClass] alloc] init];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With