views:

432

answers:

1

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

-(id)initWithCoder:(NSCoder *)coder
{
    [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.

A: 

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.

Brandon Bodnár
Just to be clear, Interface Builder is automatically subclassing my UIView class from UIViewController when I add a UIView object to the UIViewController.xib? I do see that if I exclude initWithCoder from my UIView custom class everything also loads normally, that was a useful tip.
Evan