Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`[super viewDidLoad]` convention

I see some example code with [super viewDidLoad] called before your implementation and after your implementation.

I know you don't always have to call super (as seen in many other discussions). When you do call it, is it expected before or after you code?

This could have consequences depending on what super's implementation does. Though you shouldn't have to know super's implementation to write yours.

Of course this goes for all of UIViewControllers delegate methods (willAppear, didAppear, etc...)

Any thoughts?

like image 990
Corey Floyd Avatar asked May 09 '09 22:05

Corey Floyd


People also ask

What is super viewDidLoad?

In '[super viewDidLoad]', 'super' means the same object as 'self', so the class of this 'super' is B. However, by using 'super', you're telling the compiler that you do not want to invoke class B's 'viewDidLoad' method, but its superclass's 'viewDidLoad'.

Do you need to call Super viewDidLoad?

No, you don't need to call [super viewDidLoad].

Why we use super viewDidLoad in Swift?

Based on iOS documentation and some discussions on the Internet, it would appear that viewDidLoad is called after view is loaded into memory and it's purpose is to allow for some custom setup of views loaded from nib files (basically a quote from docs).

How many times does viewDidLoad get called?

viewDidLoad() is one of the initialization methods that is called on the initial view controller. viewDidLoad() is called before anything is shown to the user - and it is called only once.


1 Answers

My rule of thumb is: if you're doing something related to initialization, always call the super class's method first (if you are going to call it at all). This gives the super class a chance to do any set-up that you may be relying on later in your method. If you're doing something related to destruction, call the super class's method last. This makes sure that you can rely on the state of the object throughout the execution of your method. Finally, take any other case on a case-by-case basis. For instance, if you're handling an event, you probably want to deal with the event first, and only invoke the super class's method if you chose not to handle the event or if you somehow altered it and want to pass it along the event chain.

like image 129
Jason Coco Avatar answered Sep 28 '22 06:09

Jason Coco