Subclassing a UIView. init method is never called Subclassing a UIView. init method is never called ios ios

Subclassing a UIView. init method is never called


Instead of init you need to call initWithCoder first then call init as :

- (id)initWithCoder:(NSCoder *)aDecoder {    if ((self = [super initWithCoder:aDecoder])) {        [self baseClassInit];    }    return self;}- (void)baseClassInit {    //initialize all ivars and properties    }


Short answer: you need to use initWithCoder:.

From the initWithFrame: docs:

If you use Interface Builder to design your interface, this method is not called when your view objects are subsequently loaded from the nib file. Objects in a nib file are reconstituted and then initialized using their initWithCoder: method, which modifies the attributes of the view to match the attributes stored in the nib file.

If you're using a nib / xib / Storyboard file, use initWithCoder:. If you're creating your view programmatically, you can use init or initWithFrame:.