So I have:
- created a couple of custom
controllers (add file
UIViewController subclass) - in Interface Builder dragged two
ViewController items from the Library
to the MainWindow.xib (ie when
applicationDelegate is, and the main
window) pane, then set their class
names to be those of the custom
controllers respectively
Now that I have done this:
-
should this then automatically take
care of instantiating these
controllers for use in the
Application Delegate’s
“didFinishLaunchingWithOptions”
method for example? -
Or do I still need to create these
controller for the Application
Delegate *.h and *.m files? -
Or in other words again, trying to
understand what IB actually does for
you when you create
customViewControllers and link them
up…
A bit more detail: when you add objects to a nib/xib file, what happens is that live objects are created, and then archived into the nib file. When you load a nib file, those archived objects are unarchived and “brought back to life” with the same settings they had when they were originally archived. Additionally, any connections that you make in the nib file between actual (UILabel, UIViewController, etc.) objects and proxy objects (“File’s Owner”) are filled in with the pointers to those newly unarchived/created objects. Note the special role of File’s Owner: the class that File’s Owner is assigned to is not created at nib load time. Instead, at the time that the nib file is loaded, e.g., with this call:
any outlets that have nil value in ownerObject (of the same class type as specified in File’s Owner) which are hooked up to objects in the nib are then set to point to those newly created objects.
You can use this fact to create self-contained and re-usable bits of View hierarchies that you load and use wherever you want: header/footers/custom cells for UITableViews, reusable-“widgets” that you create/package yourself and use repeatedly throughout the app, etc. Once you understand this, your Cocoa (touch) view programming skills go “up a level” and creating structured, modular view objects gets much, much easier and more fun!