Pradeep Reddy Kypa
Pradeep Reddy Kypa

Reputation: 4022

Why is [super loadView] or [super viewDidLoad] required?

What happens when [super loadView] or [super viewDidLoad] is written? I tried to remove the code but the stack overflows and goes into infinite loop. Can someone please explain why is this required?

Upvotes: 2

Views: 1560

Answers (3)

yuji
yuji

Reputation: 16725

First of all, when you override loadView, to create your own views manually, you should NOT call the superclass's implementation. This is because you would be creating a view manually, and using those instead of the view that would be created by UIViewController's implementation. (See the loadview documentation.)

But when you override viewDidLoad, you should indeed call the superclass's implementation. This is because UIViewController's implementation of viewDidLoad does some internal bookkeeping, and so you want to run your custom viewDidLoad code in addition to what the superclass does.

Upvotes: 4

Yusuf X
Yusuf X

Reputation: 14633

The underlying class needs to do some bookkeeping of its own to be ready before you start putting UI on top of it. In Android the corresponding app will actually intentionally crash by throwing a you-didn't-call-super-first exception.

Upvotes: 2

MCKapur
MCKapur

Reputation: 9157

It calls the method from your superclass. Lets say its NSObject. The names of these methods explain: loadView is the NSObject method which loads the view, viewDidLoad is the NSObject method which processes that the view has loaded... along those lines The method itself in your view controller "viewDidLoad" and maybe "loadView" has no code that does the above... its just for customization since you can't modify the original .m methods...

To find out your superclass go to .h file:

   @interface MyViewController : MySuperClass //thats it!

Upvotes: 3

Related Questions