Question

In most of my code, I have the following setup for viewDidLoad and viewDidUnload:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //do stuff...
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    //do stuff...
}

However, I got to wondering... does it matter when you call viewDidLoad and viewDidUnload? Should each one be before or after I "do stuff"?

In other words, should each one be at the start or end of the method?

Edit: To further complicate things, this is Apple's default viewDidUnload method, which "seems" to suggest [super viewDidUnload] be called first...

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
Was it helpful?

Solution

I do call super last in destructors and first in constructors. viewDidUnload is kind of destructor so I would call it last. But in this case it's matter of taste.

Also, just side note - viewDidUnload is deprecated since iOS 6.

OTHER TIPS

Lets say you have 2 class, a Parent and a Child. Child inherits from Parent. They have a method called greet which returns a string.

Here is what the parent method looks like:

Code:

-(NSString *)greet {
     return @"Hello";
 }

We want the child to learn from his parents. So we use super to say greet how Mommy would greet, but with our own little additions too.

Code: // Inherits from Parent

 -(NSString *)greet {
     NSString *parentGreeting = [super greet];
     return [parentGreeting stringByAppendingString:@", Mommy"]
   } 

So now Parent greets "Hello", and the Child greets "Hello, Mommy". Later on, if we change the parent's greet to return just "Hi", then both classes will be affected and you will have "Hi" and "Hi, Mommy".

super is used to call a method as defined by a superclass. It is used to access methods that have been overriden by subclasses so that the class can wrap its own code around a method that it's parent class implements. It's very handy if you are doing any sort of inheritance at all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top