ios - How do I get subclass to initialize property as its correct class? -
i have class named sppanelmanager
, has property of class, named sppanelsettingsmanager
. sppanelmanager has following in it's -init
method:
self.settingsmanager = [[sppanelsettingsmanager alloc] init];
the purpose of sppanelmanager
subclassed, , subclasses used throughout app. example, there's spgreetingmanager
. in .h file of spgreetingmanager
, have declared:
@property (nonatomic, strong) spgreetingsettingsmanager *settingsmanager;
which makes settingsmanager of correct class. problem when spgreetingmanager
subclass initialized, calls init method above, , initializes settingsmanager sppanelsettingsmanager
class, rather spgreetingsettingsmanager
.
how can initialize correct class property without having re-write init code in every subclass?
the super class (sppanelmanager
) somehow has know class concrete panel manager wants use settingsmanager
.
apple uses following approach match calayers uiviews:
the base class declares class method returns concrete sppanelsettingsmanager
subclass:
// in sppanelmanager.h + (class)settingsmanagerclass;
... subclasses override return custom class:
// in spgreetingmanager.m + (class)settingsmanagerclass { return [spgreetingsettingsmanager class]; }
now superclass can instantiate settings manager follows:
self.settingsmanager = [[[[self class] settingsmanagerclass] alloc] init];
Comments
Post a Comment