What runs before viewDidLoad?

Edward Hasted picture Edward Hasted · Nov 18, 2012 · Viewed 12.5k times · Source

I need to set and store some NSUserDefaults data that can be used withing a View. This doesn't work using viewDidLoad.

Can this be done? What method could I use before viewDidLoad? What would you recommend?

Answer

user23743 picture user23743 · Nov 18, 2012

There are a couple of methods on UIViewController that will definitely get run before viewDidLoad - which is the appropriate place for your code depends on your specific problem and architecture.

  • initWithNibName:bundle: is called when the view controller is created
  • loadView is the code that sets up the view

Another option is to ensure that the defaults are set up by a different component, before your view controller is even initialised. That could be in the view controller of a preceding part of the workflow, or initialised by the application delegate.