I’m newbie in Objective-C, and don’t understand why we need to use [super dealloc], [super viewDidLoad] or [super viewWillAppear:animated]. When I create sample code application I see something like this:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
Actually Xcode 4 always add super method at the end for every automatic generated
method. Why?
Or when I use dealloc method. Why I need to add [super dealloc] at the end?
- (void)dealloc
{
[nameField release];
[numberField release];
[sliderLabel release];
[super dealloc];
}
P.S. Now I study “Beginning iPhone 4 Development”. And don’t find any reference about this method 🙁
The point is, that you are creating subclasses by inheriting them, in your case this seems to be a custom ViewController, e.g. MyViewController which inherits data and methods from UIViewController. Inheritance means, your class will have all the methods the parent class have, even if you don’t specify them. For example:
Then the following is valid
Even if you havent declared the method, it is found in the superclass and thus the superclasses method is called.
Now suppose you have the following:
And the implementation for both is
Without a
[super doSomething], the super important method will never be called since you have overridden it in your custom implementation. So when you do athe totally important code contained in Foo’s doSomething won’t be seen by the compiler as you provide the method in your class yourself, so only Bar’s version will be called.
So whenever you override a method, you have to make sure that the base class version is called if it must be called, which is especially important for the
deallocmethod as this will release any memory the base class might have acquired during initialization.