views:

33

answers:

2

when i create a new view based application a few methods in the implementation file (.h) i do not understand their purpose and when i look into the developer center its kind of hard to understand because of how its explained.

what purpose do these methods have and what are they used for in plain english.

- (void)loadView
- (void)viewDidLoad
- (void)viewDidUnload

what im guessing the viewdidload is , is that when the view is loaded and anything between the braskets are executed first, but when there are other custom methods created (if that is the purpose of viewdidload) how does it know which method to execute? is the code executed from top to bottom? being that whatever method is listed first is executed?

also i have noticed the word super inside brackets along with other keywords. what is the purpose of super?

thank you!

A: 

Your class is a derivative of the system class UIViewController (is a subclass, in Objective C lingo). [super someMethod] means - call this method for the base class (superclass in ObjC lingo).

If you're familiar with C#, super in ObjC means same as base in C#. And the same as super in Java. :)

The system-provided UIViewController class has loading functionality in its loadView method. This method is called by the system, and it does a lot of work. The viewDidLoad method, on the other hand, is defined in the base class but does not do much. Its purpose is that you can override it and provide your own functionality there. Same for viewDidUnload.

Seva Alekseyev
A: 

Hi SarmenHB,

Well, "super" means Super Class, or the parent class that your current class extends.

@interface MyViewController : UIViewController{}

In the code above, that you can find in some .h files, MyViewController extends UIViewController. So, all the time that you call "super" inside MyViewController, you are calling the UIViewController.

So, when you see the following structure, inside MyViewController...

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

... it means that when the method "viewDidLoad" is called, it will first call the method from it's super class, to after run it's own code.

if you let the whole viewDidLoad method commented, it means that the parent method will be called automatically, because you are not overwriting it like on the previous example.

To understand what all the methods (loadView, viewDidLoad, viewDidUnload), take a look at the UIViewController Class reference on Apple Developer Connection.

Cheers,
VFN

vfn