I don't understand when I need to invoke the overriden methods or it is just not required.
I don't understand when I need to invoke the overriden methods or it is just not required.
For example, for 开发者_运维百科dealloc it is necessary
-(void) dealloc
{
...
[super dealloc];
}
For init also I guess..
-(void) init
{
[super init];
..
}
What about viewWillAppear ? and should I invoke the super method before or later my custom code ?
(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
thanks
The only way to know for sure if and when to call super
in an overridden method, is to read the documentation for that method.
For the examples you gave:
init
: The documentation states:
Subclass versions of init need to incorporate the initialization code for the classes they inherit from, through a message to super: ... Note that the message to super precedes the initialization code added in the method. This sequencing ensures that initialization proceeds in the order of inheritance.
dealloc
: The documentation states:
Subclasses must implement their own versions of dealloc to allow the release of any additional memory consumed by the object—such as dynamically allocated storage for data or object instance variables owned by the deallocated object. After performing the class-specific deallocation, the subclass method should incorporate superclass versions of dealloc through a message to super:
viewWillAppear:
The documentation states:
You can override this method to perform custom tasks associated with presenting the view.
... If you override this method, you must call super at some point in your implementation.
Every method is different. If you override viewDidAppear:, you must call super. If you override loadView, you must not. Basically, any time you override a method, you should check the documentation for that method to see if you should call super, and if so, whether you should do it before or after your own implementation.
If the documentation doesn't say, then it's up to you. After first scolding the author for his omission, do whatever you think makes sense. I'd generally be inclined not to call super if the documentation doesn't say either way.
I would say it's good practice for your super init to go first and your super dealloc to go last. Simply because you gain access and lose access, respectively, to the members of the parent function.
For a method such as viewWillAppear, it would really depend what I was going to try and do. But most likely I would call the super method first, as there's a chance the super method may override something you're trying to do in your own viewWillAppear method.
Well, that depends on whether you want to super class' method to be invoked or not.
If you do not call it at all then make sure that you know what you are doing. You may have to substitute some of the code that the super class' method would execute. However, in rare cases there might be good reasons for that.
For dealloc I would say the sequence does not matter.
For init it is rather key. In most cases your code should come after calling [super init]
.
Especially when you want to use any method of the super class yourself or when you want to read from your superclass' properties then you code should follow.
However, even here some exceptions may apply.
For viewWillAppear I am fully with Chase words. The same applies to viewDidLoad etc.
Generally, just apply some common sense. Think of which importance the superclass' method is and what it may change.
精彩评论