Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

calling viewDidLoad

  • (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.

    self.predictions = [[NSArray alloc] initWithObjects:@"You are doomed", @"Yes, I believe so", @"I doubt it", @"Neh", @"Sure", @"Nah", @"Maybe Someday", @"I suppose so.", nil];

}

without "[super viewDidLoad]", my Crystal Ball app will instantly crash when i push my button. What does that line of code do, and if i didn't include it, how in the world would i figure out that that was the error that caused my program to crash?!

2 Answers

Superclasses (the classes you inherited your subclasses from) have some pre-implemented set-up procedures that are included in their version of viewDidLoad. These methods often handle mundane but vital tasks such as initializing properties, setting up observers, setting up subviews, adjusting frames based on screen size, checking network connection, checking storyboard for outlets to hook up, etc etc etc (actual list varies depending on the superclass). Instead of guessing what needs to be done for a view to work and reimplementing all of them by yourself, you simply call their version of viewDidLoad using the super keyword, and then follow up with your own code to customize your subclass.

So, I'm relaying what my iOS instructor told my class. We all (the class) assumed [super viewDidLoad] calls the viewDidLoad method of the superclass of whichever class your view controller inherits from. He told us that it's actually informing the superclass that the view has loaded. That didn't sound right to me but he's been writing iOS apps since 3.0 so who am I to disagree? I'd like to hear from Amit Bijlani on this one. I'm sure he could give the best explanation.