views:

54

answers:

2

Hello!

I wonder why in these examples there is always a [super someMethod] inside of the Method that has the exact same name:

- (void)viewDidLoad {
    [super viewDidLoad];
    // some code
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; 
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

I know that those messages are sent to the superclass, but what's the deal with this?

+1  A: 

This is done to make sure that any superclass code (functionality and side effects) execute as well as what is in the derived class function. In effect, you are layering on top of base class functionality by doing this.

Michael Goldshteyn
+1  A: 

This is simply so that the superclass can do what it always does. It's a way for both your class and the superclass to receive the same message. In some situations, you are required to implement your own behaviour and invoke the behaviour of the superclass.

For example, the superclass will have its own memory cleanup routines which would otherwise be ignored if you did not invoke [super didReceiveMemoryWarning].

dreamlax
Thank you guys, I've got it now. :-) But I wonder, who calls the methods I re-implement in my class? I can't see anyone who sends messages to them in my files.
Steven
@Steven: The Cocoa / UIKit / AppKit frameworks send messages to your classes.
dreamlax
Ah allright, thanks again! ;)
Steven