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

In the Apple scrolling example, they don't call it. I always thought what was needed. Why should I call like that?

+47
iphone viewdidload
May 05 '09 at 12:43
source share
8 answers

If you override a method, you should still call the method in super. Even if the superclass does nothing today, Apple may one day change the implementation and your code will mysteriously stop working. If you really do not need to do anything in this method, completely leave it outside your code, and the super method will work as usual, without any intervention on your part.

+47
May 05 '09 at 1:01 p.m.
source share

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

Let’s be real: Apple is not going to break thousands of applications, including those based on their published code sample, by deciding which event they are not currently processing, it must suddenly do something that developers may or may not want to stop, and it is important if you do not need different behavior, you do not stop the event.

Edit: By watching Apple handle compatibility for an extra year, I now recommend learning and using the right pattern. Although I doubt that your application binary will ever suddenly stop working, it is clear that the iPhone detects which SDK was created for your binary and changes the behavior of the OS based on this.

One day, Apple may require that a particular template be executed in the future SDK. This will not affect you until you rebuild with the latest Xcode + SDK, but then you get these breaks without changing the source code. Study and follow the pattern to be safe.

+27
Sep 11 '09 at 2:20
source share

As Marcus says, the UIViewController does nothing in its viewDidLoad method, so you don't need to call it. However, it’s a good habit to join if you change the inheritance structure and suddenly the class that inherits from the UIViewController now inherits from something that does something in the viewDidLoad method.

+16
May 05 '09 at 12:56 a.m.
source share

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

+10
Feb 14 '11 at 19:34
source share

Let's say you have 2 classes, a parent and a child. The child inherits from the parent. They have a greet method that returns a string.

Here's what the parent method looks like:

the code:

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

We want the child to learn from his parents. Therefore, we use super to welcome how mom will welcome, but also with our own little additions.

code: // Inherited from parent

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

So, now Parent welcomes "Hello", and Child welcomes "Hello, Mommy". Later, if we change the parent hello to only return “Hello,” then both classes will be affected, and you will have “Hello” and “Hello, mom.”

super is used to call the method defined by the superclass. It is used to access methods that have been overridden by subclasses so that the class can wrap its own code around a method that implements its parent class. This is very convenient if you don’t inherit anything at all.

+10
Oct 21 '14 at 11:33
source share

You do not need to call [super viewDidLoad]

As far as I know, viewDidLoad in the superclass (UIViewController) is just an empty function that is called when the ViewController is initialized using the nib file.

So, if you need to perform any initialization, you must override this function and put your code there.

+9
May 05 '09 at 12:54
source share

I just noticed that the Xcode 6 parser gives a warning if you do not call super in these functions. So it seems like Apple now definitely wants us to call it.

+5
Jul 6 '14 at 1:33
source share

Although in xCode 7 Beta / Swift 2 super.viewDidLoad will not compile. The error says that it is only available in osx 10.10, and auto-fix does it

 if #available(OSX 10.10, *){ super.viewDidLoad()} else { // Fallback on earlier versions } // My code } 
0
Jun 20 '15 at 11:04
source share



All Articles