Question

In this code I am loading a View Controller (and associated View) from a .xib:

-(id)initWithCoder:(NSCoder *)coder
    {
    // add custom initialisation code here
    [super initWithCoder:coder];
    return self;
    }

This successfully works, but I do not really understand what the line [super initWithCoder:coder] is accomplishing. Is that initializing my View Controller after my View has been initialized?

Please be as explicit as possible when explaining. Thanks.

Was it helpful?

Solution

Your class is a subclass of UIViewController. The call is telling your super class (UIViewController) to do the steps it needs to accomplish so that you can do your init steps. This would be setting up any properties that the UIViewController provides or registering for notifications that the UIViewController needs to do its work.

It is suggested almost every time you override a method from the super class to call the super class's method in addition to the steps you need to take.

Edit: Also if you don't need to do anything in a method the superclass provides, you can just leave it out and the super class's method will be used instead. In this case I would not provide the initWithCoder: method unless there was some code you need to preform in addition to what you showed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top