2

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];

1 Answer 1

7

CustomViewChild shouldn't call initMyStuff. Just call this in the parent and the child's initMyStuff should call [super initMyStuff].

But in either case, don't call it initMyStuff because that suggests it's an init method, which implies certain behaviors. Call it something like setup (assuming the goal is to have it called from both initWithFrame and initWithCoder:).

Sign up to request clarification or add additional context in comments.

2 Comments

If the parent class was an NSObject, would there be any problems by having the parent class do this: if(self = [super init]) instead of if(self = [super initWithCoder:aDecoder])? This would call the default init methods then go back down the chain and set the decoded variables.
If the parent class were an immediate subclass of NSObject, you would have to do it that way. NSObject doesn't implement <NSCoding>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.