开发者

When overriding initWithCoder is it always necessary to call [super initWithCoder: coder]

开发者 https://www.devze.com 2022-12-22 23:20 出处:网络
In this code I am l开发者_如何转开发oading a View Controller (and associated View) from a .xib:

In this code I am l开发者_如何转开发oading 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号