views:

31

answers:

2

A new iOS ViewControllers created from a template contains several "boilerplate" methods that call their parent class methods.

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

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

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

When modify these classes, should I put my own code before or after the parent class calls?

- (void) viewDidLoad {
        // Should I put my code here?
        [super viewDidLoad];
        // Or here?
}
+2  A: 

In viewDidLoad (and generally) you should go after, cause its calling the load method on the parent class

In dealloc you'd go before

tigermain
+1  A: 

This is applicable for all OOP in general. In constructor (and in other methods too) you should call the parent's constructor before your code. The reason is your code may require some initialization which are handled in parent, i.e. initialization of base should go before initialization of derived class. In destructor you should do the opposite, i.e. releasing of derived class's resources should go before releasing resources of base. The reason is straight forward. Derived class's resource may depend on base's resource. If you release resource of base before then there might be trouble.

This is the ideal case. In many cases you may see no difference but if there is dependency like described above then you will be in trouble. So you should follow the standard, call base class's method before your code and in dealloc do the opposite.

taskinoor
In Objective-C it is essential that -init methods call super first _and_ assign the result to self--initializers are permitted to return a _different_ object than the object they were called on.
rpetrich