martes, 10 de enero de 2012

What is "super" method and how to use it

Probably if you are new with Objective-C you'll find that in execution-cycle methods, is called a method named "super".
This method is used to called a method of the super class, parent of current using class.
For example if we have a class called MyTest , which inherits from UIViewController, we will have in .h file something like this.

  1. #import <UIKit/UIKit.h>
  2. @interface MyTest : UIViewController {
  3. }

And one of the execution-cycle methods could be

  1. - (void)viewDidLoad {
  2.     [super viewDidLoad];
  3. }

And why we have to call the same method inside the parent class? Because we have to instantiate and perform all the instructions to declare and continue with execution-cycle inside UIViewController, our parent super class.

One must-know thing, is that the order of this call inside of viewDidLoad is not the same , when calling constructors or initializations "[super]" methods must be at the top of the implementation, because the first thing to do is the initialization of the parent class.
When using destructors, like dealloc method, "[super]" should be at the bottom of the implementation, because we need to free all our objects, and at last, free the parent super class.

No hay comentarios:

Publicar un comentario