I have two UIViews. They both require custom initialization, and it seems repetitive and bug-prone to simply copy initialization code in both initWithFrame: and initWithCoder:. However, having a standard initialization method name like initMyStuff results in the child's version being called twice. I could resolve this by giving each class a unique initialization name like initStuffForCustomViewParent, but I would prefer if there was a way to maintain a standard custom initialization method name in order to make my code more readable. Here is a sample implementation of the problem:
@implementation CustomViewParent
-(id)initWithFrame:(CGRect)aRect
{
if(self=[super initWithFrame:aRect])
[self initMyStuff];
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if(self = [super initWithCoder:aDecoder])
[self initMyStuff];
return self;
}
-(void)initMyStuff
{
// Do stuff (never called)
}
@end
@implementation CustomViewChild
-(id)initWithFrame:(CGRect)aRect
{
if(self=[super initWithFrame:aRect])
[self initMyStuff];
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if(self = [super initWithCoder:aDecoder])
[self initMyStuff];
return self;
}
-(void)initMyStuff
{
// Do stuff (called twice)
}
@end
// SAMPLE
// Calls CustomViewChild's initMyStuff twice and CustomViewParent's initMyStuff never
CustomViewChild *obj = [[CustomViewChild alloc] init];