when exactly do Interface Builder items get instan

2019-05-24 07:52发布

问题:

Say I create a navigation based application from the template in XCode4, then there will be in the MainWindow.xib a Navigation Controller, which has as a child the RootViewController.

When exactly would then:

  1. Instance of RootViewController be created?
  2. This instance be associated as a child with the Navigation Controller?

In particular when in relation to the timing for the applicationDelegate "didFinishLaunchingWithOptions" method and when it occurs.

回答1:

As given in the plist the MainWindow is the Main nib file base name. So there is some hidden code which will be generated based on the plist to load the main window nib file on startup. This happens before didFinishLaunchingWithOptions.

As soon as the MainWindow nib is loaded there is a cascade of things that are done in the background, please refer to The Nib Object Life Cycle in the Resource Programming Guide.

One of those steps is

It unarchives the nib object graph data and instantiates the objects.

Then almost finally it does:

It sends an awakeFromNib message to the appropriate objects in the nib file that define the matching selector: ... In iOS, this message is sent only to the interface objects that were instantiated by the nib-loading code. It is not sent to File’s Owner, First Responder, or any other proxy objects.

The first method you can get a grip on is awakeFromNib.

To answer your three questions:

  1. During loading of the MainWindo nib file
  2. Yes, have a look at the nib file in interface builder
  3. It all happens before didFinishLaunchingWithOptions


回答2:

All that will be accomplished before the code reaches application:didFinishLaunchingWithOptions:. The UIApplicationMain() function (called from your app's main() function loads MainWindow.nib. When a NIB file is loaded, all the objects in the NIB file get instantiated and the connections between the objects are made.

Note that this means that the view controllers themselves already exist in application: didFinishLaunchingWithOptions:. The same is not true for the view controller's view. A view controller loads its view lazily the first time it is accessed.