title: Why is viewDidLoad called twice when the rootViewController property of UIWindow is set?
body: <p>There were already a couple of similar questions, but it wasn’t exactly the same as in my case. Just start a new view based project, add <code>viewDidLoad</code> and <code>awakeFromNib</code> with NSLog statements and do the following changes in <code>MainWindow.xib</code>:</p>
  
  <ul>
  <li>Remove the view controller connection in the application delegate.</li>
  <li>Connect <code>UIWindow’s</code> <code>rootViewController</code> delegate to the App View Controller instance.</li>
  </ul>
  
  <p>The stack trace from the first call:</p>
  
  <pre><code>#0     0x000025c0 in -[foozaViewController viewDidLoad] at /Users/rafael/Downloads/fooza/fooza/foozaViewController.m:38
  #1     0x000cd089 in -[UIViewController view] ()
  #2     0x00040d42 in -[UIWindow addRootViewControllerViewIfPossible] ()
  #3     0x0079d5e5 in -[NSObject(NSKeyValueCoding) setValue:forKey:] ()
  #4     0x00050ff6 in -[UIView(CALayerDelegate) setValue:forKey:] ()
  #5     0x0021930c in -[UIRuntimeOutletConnection connect] ()
  #6     0x00d418cf in -[NSArray makeObjectsPerformSelector:] ()
  #7     0x00217d23 in -[UINib instantiateWithOwner:options:] ()
  #8     0x00219ab7 in -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] ()
  #9     0x0001f17a in -[UIApplication _loadMainNibFile] ()
  #10     0x0001fcf4 in -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] ()
  #11     0x0002a617 in -[UIApplication handleEvent:withNewEvent:] ()
  #12     0x00022abf in -[UIApplication sendEvent:] ()
  #13     0x00027f2e in _UIApplicationHandleEvent ()
  #14     0x01004992 in PurpleEventCallback ()
  #15     0x00dac944 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
  #16     0x00d0ccf7 in __CFRunLoopDoSource1 ()
  #17     0x00d09f83 in __CFRunLoopRun ()
  #18     0x00d09840 in CFRunLoopRunSpecific ()
  #19     0x00d09761 in CFRunLoopRunInMode ()
  #20     0x0001f7d2 in -[UIApplication _run] ()
  #21     0x0002bc93 in UIApplicationMain ()
  #22     0x000020d9 in main ()
  </code></pre>
  
  <p>And here’s the trace from the second call:</p>
  
  <pre><code>#0     0x000025c0 in -[foozaViewController viewDidLoad] at /Users/rafael/Downloads/fooza/fooza/foozaViewController.m:38
  #1     0x00002555 in -[foozaViewController awakeFromNib] ()
  #2     0x00217f26 in -[UINib instantiateWithOwner:options:] ()
  #3     0x00219ab7 in -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] ()
  #4     0x0001f17a in -[UIApplication _loadMainNibFile] ()
  #5     0x0001fcf4 in -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] ()
  #6     0x0002a617 in -[UIApplication handleEvent:withNewEvent:] ()
  #7     0x00022abf in -[UIApplication sendEvent:] ()
  #8     0x00027f2e in _UIApplicationHandleEvent ()
  #9     0x01004992 in PurpleEventCallback ()
  #10     0x00dac944 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
  #11     0x00d0ccf7 in __CFRunLoopDoSource1 ()
  #12     0x00d09f83 in __CFRunLoopRun ()
  #13     0x00d09840 in CFRunLoopRunSpecific ()
  #14     0x00d09761 in CFRunLoopRunInMode ()
  #15     0x0001f7d2 in -[UIApplication _run] ()
  #16     0x0002bc93 in UIApplicationMain ()
  #17     0x000020d9 in main ()
  </code></pre>
  
  <p>Is this desired behavior or a bug? Should we still use the more traditional
  way of connecting an outlet to the root view controller in the
  application delegate to maintain a sane <code>viewDidLoad</code>/<code>viewDidUnload</code> ratio
  or should we ignore Apple’s rules and do not call super in <code>awakeFromNib</code> and use the new <code>rootViewController</code> property of <code>UIWindow</code>?</p>