Beginning iOS Development

Course No. 10-152-143

IB, File’s Owner, and Nibs

Just what the heck is a “nib”? And I’m really confused about “File’s Owner”!

Understanding “nib” Files

  • When we click on a file that ends with .xib, we open the file in Interface Builder.

  • In this editor we can create our user interface view and wire up outlets and actions. What is this file and what is happening when we edit it?
  • Let’s look at the file first. Control-click on a .xib file and select Open As→Source Code.

  • Here’s the top of the file.
  • What? That’s just an XML document!
  • Exactly, that’s what a “nib” file really is. In the early days of NextStep and Mac OS X, the files were in a binary format and had the extension of .nib (NextStep Interface Builder). Most iPhone developers still refer to these files as “nib files”. However, they are now in an XML format and have the .xib extension.
  • Let’s have a closer look at a simple nib file. Here’s is the xml for the text field at the top of the screen.
    • All this for one text field? Yup!
  • So, what is this? What are nib files? How are they created? What are they for?
  • Notice the element type? It is textField and that is a hint!
  • <drum-roll>
  • The nib files are serialized objects.
  • </drum-roll>
  • Ah, ha! Um, what?


Getting Closer

  • Let’s look at this in detail.
  • Here’s a simple class in Obj-C. Apple.h:
  • And Apple.m:
  • If want to use some instances of this class we would create some objects like this.
  • This would give us real objects!

  • And, we can only have these actual objects while the application is running, right?
  • Er, well, yes and no. In a real application this is true. But, Interface Builder does something really cool, it creates real objects!
  • When you drag something from the Library palette, IB is actually instantiating a real object of that type.

  • IB performs the [[<SomeClass> alloc] init] for you and holds the object internally. Cool!
  • When you change the attributes of the object you are actually changing values in the real object:

  • When you save the nib file the live objects are being serialized to the XML file we see above.
  • OK, I see all this, so what’s the File’s Owner?
  • Ah, yes, File’s Owner…

File’s Owner

  • I’m getting there! Here’s the issue. When you are in IB, you are creating and manipulating real Objective-C objects:

  • However, over here in Xcode you are still just working with a text file. There’s no running program. How do you use live objects in IB with plain old text in Xcode?

  • How can they work together?

  • The answer is that they can’t and they don’t. Um, What?
  • That’s right, it’s not possible for them to work together until the text files have been compiled and your application is running.
  • OK, what am I missing here?
  • What’s missing is File’s Owner:

  • <drum-role>
  • This magic thing called the File's Owner is a placeholder, that pretends to be your real object in your running application while you are making your real application.
  • </drum-role>
  • Now we might be getting somewhere!
  • In the case of this nib file, ViewController.xib, the File’s Owner proxy is pretending to be an object of the ViewController class and all its contained objects.
  • Here’s what happens. When your application is launched, an object from the ViewController class is instantiated. Then the magic happens! The nib file is loaded into RAM and converted back to real live objects, it’s deserialized. All the objects you see in the above image are created and all the Outlets have their pointers filled.
  • While we are editing our interface code in IB, we are making live objects and then hooking them up to the placeholder called File’s Owner.
  • How does IB know about outlets and actions?
  • Good question and very timely!

File’s Owner and its Class

  • A File’s Owner has to have a type setting, which is the class it is pretending to be:

  • It can pretend to be this class so well because IB has access to your source code:

  • What’s it’s really interested in are the items marked with IBOutlet and IBAction. They don’t start with “IB” by accident! Those #defines were created for IB.
  • Once you’ve identified the type of File’s Owner IB immediately parses the .h file and finds the outlets and actions.

View Controllers and Their Nib Files

  • Hopefully, this is starting to makes some sense. We have View Controllers and they will have a view. This view can be made all in code. It doesn’t need IB. However, it is much easier to make a visual interface with a visual tool like IB.
  • View controllers will load their nib file at launch if there is one.