Your question is actually a more generic question that can be removed from the dealloc
issue.
Overriding methods and when you should call super
.
Sometimes when you are subclassing you override an existing method. Sometimes you want to use that convenient time to perform some behavior, sometimes you want to prevent the parent from doing something and change behavior.
- (void)methodOne {
// don't let silly parent class do method one stuff
// do my stuff …
}
- (void)methodTwo {
// let parent perform his behavior first
[super methodTwo];
// do my stuff …
}
- (void)methodThree {
// do my stuff …
// let parent do his stuff after I have
[super methodThree];
}
- (void)methodFour {
// it's not common, but you might want to split your stuff with a call to
// super - this usually involves knowledge of what super is up to
// do some stuff …
[super methodFour];
// do my stuff …
}
The documentation for many methods/classes (see UIView and NSManagedObject) might say whether you can or should or shouldn't override methods. Good documentation will tell you when you should invoke super.
When invoking [super dealloc]
you should invoke it last, after you have released the resources you have a hold on. (because it's likely that other memory you might reference will be released by your parent class).
Another oft invocation of super
is in init methods. If you implement an init method, you should override the parent's "designated initializer", and you should call super from yours.
@implementation CustomViewController
- (id)init {
return [super initWithNibName:@"CustomView" bundle:nil];
}
- (id)initWithNibName:(NSString *)name bundle:(NSBundle *)bundle {
return [self init];
}
//…
This common pattern prevents anyone from loading your class with the incorrect nib file. (This may or may not be desired, that's another issue.)
If there happened to be other initializers, by definition they should invoke their designated initializer. So if UIView were to add a initWithNibName:
method, it would likely invoke [self initWithNibName:name bundle:nil]
which would then be "caught" and redirected to your intended initializer.