R – What’s the missing step/information in this iPhone Interface Builder tutorial

interface-builderiphoneobjective cxcode

I'm following a tutorial on how to use Interface Builder from Chapter 2 of Zdziarsky's "iPhone SDK Application Development." The idea appears to be to create a very basic app which consists of a Tab Bar Controller which has two buttons, each tied to a different view object (and each view object in its own respective .xib file). What I end up with is a blank window and some code that's significantly different from what the book indicates I should have.

I suspect a missing step, but the instructed steps are scattered across a chapter of explication, so I'm not 100% sure I've haven't missed something. Here's a summary of what the steps appear to be:

  1. Create a new "window-based application" named "Example."
  2. Open MainWindow.xib in Interface Builder and do the following:
    a) Select the "Window" icon in the document window, open the inspector, set various extraneous properties.
    b) Drag a "Tab Bar Controller" into the document window.
    c) Edit the two tab names to "Recents" and "History."
    d) Save MainWindow.xib
  3. Create a new "Cocoa Touch View" IB template. Add a "Table View" to it. Save it as Recents.xib.
  4. Similarly, create another "Cocoa Touch View" IB template. Add a "text view" to it, save as History.xib.
  5. Go back to MainWindow.xib, edit each of the tabs in the Tab Bar Controller in the Inspector so that the "Nib Name" corresponds to the appropriate .xib file created in step 3 (for Recents) and step 4 (for History).
  6. Add Recents.xib and History.xib to the project "Example."

At this point, the book says that if I look at ExampleAppDelegate.h, I should see links to the Interface Builder objects in my code, such as:

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet RootViewController *viewController;

When I actually look at the file, though, I only see the first (UIWindow) line above. The RootViewController line is absent.

Similarly, the book says that when I look inside of ExampleAppDelegate.m, I should see some synthesize directives:

@synthesize window;
@synthesize viewController;

But again, I only see the line relevant to window.

And sure enough, if I click "Build and Go," what I see in the simulator is an empty window.

One of the things I noticed while going through this process was that I wasn't instructed to drag the Tab Bar Controller from step 2b directly into the displayed Window object in the interface builder. I tried to do this anyway and found that I wasn't allowed to (and in fact, that view controllers of any kind didn't seem welcome directly in the Window). This seemed odd to me — I noticed that directly dragging a View object into the Window seemed OK. But since Views can't contain View Controllers, I can't see how you're ever supposed to add a View Controller inside of a heirarchy contained in a Window.

What steps and/or pieces of knowledge am I missing here?

Best Answer

The window and viewController outlets are added as part of the project template, so perhaps XCode's templates have changed from when the tutorial was written. XCode now has separate templates for tab bar, navigation, and view-based apps, so you should take a look at those.

With the plain Window-based app template you would need to add the rootViewController outlet in your AppDelegate header file, and connect it to your view controller instance in IB. Your app delegate would also need code to add your main view to the window:

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    // Add the tab bar controller's current view as a subview of the window
    [window addSubview:rootViewController.view]; 
}

That's how your main view controller relates to your app's window. Without that code, you will only see an empty window.

Related Topic