itenyh
itenyh

Reputation: 1939

Why 'NSInternalInconsistencyException' happens when overwrite 'initWithCoder' method

I came across the exception 'NSInternalInconsistencyException' when I write the code below in a viewcontroller which is associated with storyboard:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.view.backgroundColor = [UIColor blackColor];
    }
    return self;
}

However, if I delete the line 'self.view.backgroundColor = [UIColor blackColor];', there is no problem. I don`t know the reason.

Actually, in the stroyboard I have several controllers, and I want to make a base controller for all the controllers. The purpose is that I want to set a background view(UIView) for them.

I try to add a UIView in the viewDidLoad method of the basecontroller, but the view covers any components that set in the storyboard. I think the reason is that the components on the sub viewcontrollers are initialized in the 'initWithCoder' method before the 'ViewDidLoad' method is called. So I try to add the bgView in the 'initWithCoder' method of the basecontroller, and this led to the problem above.

So I also want to know a right way to achieve my purpose. Thanks!

Upvotes: 1

Views: 57

Answers (1)

lead_the_zeppelin
lead_the_zeppelin

Reputation: 2052

dont set graphics related properties in init and bring subview to front. Set them from

-(void)awakeFromNib
{
    [self.view.superview bringSubviewToFront:self.view];
    self.view.backgroundColor = [UIColor blackColor];
}

Upvotes: 1

Related Questions