Chris Dzombak

sharing preview •

Working with custom UIView designated initializers

I have a UIView subclass, with a custom designated initializer. How should I handle the requirement to override initWithCoder:?

Working with custom UIView designated initializers

Update Jan. 6, 2016: I should’ve written this update earlier, so I could remember and thoroughly explain my train of thought. But for future reference: after this Twitter discussion with Matt, you can see the approach I chose for the affected classes in our open-source photo viewer:

I have a UIView subclass, with its own custom designated initializer. (Specifically, I’m talking about NYTPhotoCaptionView from our open-source photo viewer.)

As you might expect, under Xcode 7 this produces a warning:

Method override for the designated initializer of the superclass ‘-initWithCoder:’ not found

But I’m not sure of the best way to resolve this warning.

My first attempt was to create an -initWithCoder: implementation which would call through to super, then perform common initialization:

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];

    if (self) {
        [self commonInit];

    return self;

But this results in another 2 warnings:

Convenience initializer should not invoke an initializer on ‘super’

Convenience initializer missing a ‘self’ call to another initializer

I guess that makes sense. But this means I always have to call my view’s custom designated initializer, which doesn’t support NSCoder.

So as I see it, these are my options:

What’s the right approach here? Get at me; I’m @cdzombak on Twitter.