Question

Apple doc says:

Before the initial view controller is displayed, your app delegate is called to give you a chance to configure the view controller.

But that is not actually happening.

  1. I created project in Xcode with single view template.
  2. In ViewController.m I added awakeFromNib method and inside I just NSLog(@"VC").
  3. Then in AppDelegate in didFinishLaunchingwithOptions I just NSLog(@"AD")
  4. When I am running the project ViewController execute first then AppDelegate.
  5. With out storyboard didFinishLaunchingwithOptions method always execute first and I was able to configure database and other initial stuff that all ViewControllers can use but now I am not able to do that.
  6. ViewController.m is executing first and my database setup is in didFinishLaunchingWithOptions.

What is going wrong?

Was it helpful?

Solution

Nothing is going wrong. awakeFromNib is called when the storyboard is loaded. The documentation states "before the initial view controller is displayed".

You are probably doing things in awakeFromNib that are not appropriate, but nobody knows without more information

The app lifecycle with storyboards is slightly different to using nibs. With nibs, you'd create and populate the initial view controller in didFinishLaunching. With storyboards, that's done for you, before didFinishLaunching is called. You can then access self.window.rootViewController in that method to get hold of your initial view controller.

The point remains that awakeFromNib is not a good place to be doing anything like responding to databases being set up or querying values. Your view hasn't been loaded yet. You should be doing these things later in the lifecycle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top