4

I've read Apple's "Your first iOS application" guide and everything there seems crystal clear to me. However, when I try to understand how View-based iOS application template provided in XCode works, I run into some interesting conundrums.

I understand that the application gets the main nib file name (usually, the MainWindow.xib) form the *-Info.plist file. What I do not understand is, how does the XCode know which nib file is associated with the controller that is created with this View-based application template by default. In the guide, you start with the Window-based application, and you "have to write" something like:

MyViewController *aViewController = [[MyViewController alloc]
initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];

[self setMyViewController:aViewController];

which makes perfect sense. However, it turns out that in the View-based iOS application template there is no such thing, and that this nib specification was not actually needed in the first place, as long as you created your UIViewController subclass with option "With XIB for user interface" checked. My question is, how does XCode know which nib is associated with this controller, i.e. is it storing this connection in some of the files, or maybe by some sort of convention (same name for controller and nib file, perhaps)? Moreover, where does that 'Loaded from "MyViewBasedAppController"' subtitle come from in Interface builder's view of controller within MainWindow.xib? It's definitely not there when I add the controller by hand, so I'm curious to what magic does XCode do behind my back, when I think I'm just selecting a simple code template.

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
itim
  • 312
  • 2
  • 11

1 Answers1

6

If you look in the target info (double click on the target to bring that up), 'Properties' tab you'll see the name of the main Nib file. The words 'Nib' and 'Xib' are interchangeable for these purposes; Xib is just a newer alternative for Nib.

It'll be 'MainWindow' fresh from the template. If you open MainWindow.xib you'll see that in there is an object called '[project name] App Delegate', and if you show the inspector and check under the 'i' tab you'll see the type of class that is named at the top. If you check the connections tab (the right facing arrow), you'll see that the File Owner (which is the UIApplication itself) has its 'delegate' property attached.

You'll also see that it has an outlet called 'viewController'. That's attached to another object in the xib called '[project name] View Controller'. Check the type on that and you'll see it's the type of view controller that Xcode has added to your project. Looking at its attributes (the first tab in the inspector, with the slider graphic), you'll also see that a separate nib file is specified as containing its main details.

For argument's sake, suppose I called my project 'NibTest' and made no changes.

At runtime, the device loads Info.plist. In there it sees that the delegate is of type NibTestAppDelegate. So it'll instantiate an instance of the class NibTestAppDelegate and set the UIApplication's delegate property to it.

It'll then see from MainWindow.nib that NibTestAppDelegate has a member named viewController of type NibTestViewController. So it'll create an instance of that and set the viewController property on the NibTestAppDelegate instance it just created to it.

In doing that it'll open the other xib and continue doing the same sort of steps.

Objective-C has a fully reflective runtime, so you can instantiate objects by their class name at runtime. This is one of the differences between Objective-C and C++, for example.

Xcode isn't generating any hidden code or relying on any hidden naming conventions. The whole thing is figured out at runtime by the OS.

EDIT: for example, in place of your example:

    MyViewController *aViewController = [[MyViewController alloc]
initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];

You could actually do:

    MyViewController *aViewController = [[NSClassFromString(@"MyViewController") alloc]
initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];

They'll operate identically as long as MyViewController exists in the program or in the wider runtime.

You could alternatively pass any other string object you like to NSClassFromString. Even ask the user for it if you want (though it'd be a really bad idea for security reasons).

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
Tommy
  • 99,986
  • 12
  • 185
  • 204
  • Excellent explanation of the whole process, thumbs up. – Matthew Frederick Nov 15 '10 at 03:29
  • Well, you are perfectly right about instantiating classes from their name, but that is not quite what I asked. What you say here: "In doing that it'll open the other xib and continue doing the same sort of steps." doesn't seem to answer my question: how is it resolved that the nib file for NibTestViewController controller was that very NibTestViewController.xib file? Of, to put it differently, how is it known that there should even be any nib file for loading in this case (since we could clearly instantiate controller without the nib file)? – itim Nov 15 '10 at 06:56
  • "...Looking at its attributes (the first tab in the inspector, with the slider graphic), you'll also see that a separate nib file is specified as containing its main details" -- sorry, I was really tired when reading this. This answer actually nails it, thank you a lot! – itim Nov 15 '10 at 09:21
  • I tried using NibTestViewController in MainWindow.xib _without_ specifying "NIB Name" under "Attributes", and it still loads correct view (the one from the NibTestViewController.xib). It works even if I change filename to, say, NibTestViewControllerObscureMe.xib. How does iOS _still_ know which nib file is associated with this controller? In this case, is it maybe going through all of the nibs in the project, until it finds the one that has the correct "className" in "IBPartialClassDescription"? – itim Nov 15 '10 at 16:32
  • Are you sure it's actually loading anything from the second nib? It'll know what sort of class to create from the first one - the second one just contains proper details. – Tommy Nov 16 '10 at 00:24
  • Well, it sure seems like it does (when I change the view's background color to green, for example, the change is visible in the application). However, when I create the controller class with "with nib file" left unchecked and add the nib manually later, proper view does not get loaded until I specify "nib file" explicitly under controller's attributes. But it doesn't matter, I guess, for even if it works, it's probably not meant to be used in such way, and you already made the whole matter pretty clear. – itim Nov 16 '10 at 01:13