ViewDidLoad / Unload messages to super?

前端 未结 4 1989
走了就别回头了
走了就别回头了 2021-01-19 15:44

I have noticed code from other sources where the author has not added the messages to super that are shown below. I usually add them both, but I was just curious as to what

4条回答
  •  星月不相逢
    2021-01-19 16:18

    You should only override a method when you need to perform some kind of action, so I'm assuming your code really looks something like this:

    -(void)viewDidLoad {
        //do things that need to be done when the view is loaded
        [super viewDidLoad];
    }
    

    If you think the parent if your view needs to do something as well, then a call to [super viewDidLoad]; is definitely in order. If you look at the documentation for viewDidLoad, however, it states

    This method is most commonly used to perform additional initialization steps on views that are loaded from nib files

    The word "additional" here is crucial: it's just a convenience method. This is in contrast to dealloc, where the documentation states explicitly that when overriding dealloc you must always finish with a call to [super dealloc].

    If your implementation of viewDidLoad and viewDidUnload really don't do anything else but call super, you should remove them: they accomplish nothing apart from making extra work for the compiler.

提交回复
热议问题