Do I always have to call [super viewDidLoad] in the -viewDidLoad method?

IphoneViewdidload

Iphone Problem Overview


In Apple's scrollView example they don't call that. I always thought that's a must. Why should I call that anyways?

Iphone Solutions


Solution 1 - Iphone

If you are overriding the method you should still call the method in the super. Even if the super class is not doing anything with it today, Apple might one day change the implementation and your code will mysteriously stop working. If you really don't need to do anything in that method, leave it out of your code entirely, and the super's method will run as usual, without any intervention on your part.

Solution 2 - Iphone

No, you don't need to call [super viewDidLoad]. Edit: But read below, because I think you definitely should.

Let's be real here: Apple is not going to break thousands of apps, including those based on their published sample code, by deciding an event they're not currently handling suddenly needs to do something that developers may or may not want to stop and it's critical that if you don't need different behavior you not stop the event.

Edit: Having watched how Apple handles compatibility for an extra year, I now recommend learning and using the correct pattern. While I doubt your application binary will ever suddenly stop working, it's clear that the iPhone detects which SDK your binary was built against and modifies some OS behaviour based on this.

Apple might one day require a particular pattern be followed on some future SDK. This would not affect you until you rebuild with the latest Xcode + SDK, but then you'd get these breaks without any source code changes. Learn and follow the pattern to be safe.

Solution 3 - Iphone

As Markus says, UIViewController doesn't do anything in its viewDidLoad method, so you don't have to call it. However, it's a good habit to get into, in case you change your inheritance structure and suddenly the class that used to inherit from UIViewController now inherits from something that does do something in the viewDidLoad method.

Solution 4 - Iphone

Lets say you have 2 class, a Parent and a Child. Child inherits from Parent. They have a method called greet which returns a string.

Here is what the parent method looks like:

Code:

-(NSString *)greet {
   return @"Hello";
}

We want the child to learn from his parents. So we use super to say greet how Mommy would greet, but with our own little additions too.

Code: // Inherits from Parent

-(NSString *)greet {
  NSString *parentGreeting = [super greet];
  return [parentGreeting stringByAppendingString:@", Mommy"]
}

So now Parent greets "Hello", and the Child greets "Hello, Mommy". Later on, if we change the parent's greet to return just "Hi", then both classes will be affected and you will have "Hi" and "Hi, Mommy".

super is used to call a method as defined by a superclass. It is used to access methods that have been overriden by subclasses so that the class can wrap its own code around a method that it's parent class implements. It's very handy if you are doing any sort of inheritance at all.

Solution 5 - Iphone

Apple's documentation for viewDidLoad does NOT state that you should call [super viewDidLoad], so I would go with what Apple's says. Note, however, that for other similar methods like viewDidAppear, you must call [super viewDidAppear].

Solution 6 - Iphone

You don't have to call the [super viewDidLoad]

As far as I know, the viewDidLoad in the superclass (UIViewController) is only an empty function that gets called when the ViewController gets initialized with a nib-file.

So if you need to do any initializing, you should override this function and put your code there.

Solution 7 - Iphone

Just noticed that the static analyzer of Xcode 6 issues a warning if you do not call super in these functions. So it seems Apple now definitely wants us to call it.

Solution 8 - Iphone

Although in xCode 7 Beta/Swift 2 super.viewDidLoad won't compile. The error says it's only available in osx 10.10 and the auto-fix does this

if #available(OSX 10.10, *){
super.viewDidLoad()}
else
{
// Fallback on earlier versions
}
// My code
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThanksView Question on Stackoverflow
Solution 1 - IphoneGeorge ArmholdView Answer on Stackoverflow
Solution 2 - IphoneSteven FisherView Answer on Stackoverflow
Solution 3 - IphonePaul TomblinView Answer on Stackoverflow
Solution 4 - IphoneKishore SutharView Answer on Stackoverflow
Solution 5 - IphoneDanielView Answer on Stackoverflow
Solution 6 - IphoneullmarkView Answer on Stackoverflow
Solution 7 - IphoneKlaus ThulView Answer on Stackoverflow
Solution 8 - IphoneianView Answer on Stackoverflow