Setting Up a Nib for your iPhone or iPad - dummies

Setting Up a Nib for your iPhone or iPad

By Neal Goldstein, Jon Manning, Paris Buttfield-Addison

Part of iPhone and iPad Game Development For Dummies Cheat Sheet

Nib files end in .xib and are used in Interface Builder to construct the screens of your game application for your iPhone or iPad.

To start building a nib file, double-click it in Xcode to open the file in Interface Builder. You can then drag views and controls, such as images and buttons, into the nib file.

To make your code aware of when the user taps buttons on your screen, follow these steps:

  1. For each button on your screen, add your action methods to the .h file for your view controller.

    Action methods look like this:

    - (IBAction) doSomething;
  2. Connect the button to the nib by following these steps:

    1. Go back to the nib file in Interface Builder, and open the Document Window by pressing Command+0.

      You see a file’s owner entry at the top of the list.

    2. Hold down the Control key, and click and drag from the button onto the file’s owner.

      A list of action methods you can attach the button to appears.

    3. Click the method you want.

  3. Add the method code that runs when the button is tapped to the .m file for your view controller.

    This code looks something like this:

- (IBAction) doSomething {
  // This code is run when the button is tapped

You might also want your code to be able to refer to the views in your nib. To do this, you need to have an outlet variable for each view you want to refer to.

To set up the outlet variable, follow these steps:

  1. Add an outlet variable to store the reference to the control and put it in the .h file for your view controller, between the curly braces.

    Outlet variables look like this:

    IBOutlet UIButton* myButton;
  2. Connect the control to the outlet variable by opening the Document Window, holding down the Control key, and dragging from the File’s Owner onto the control.

    Now whenever you use the myButton variable, you work with the control you connected it to in the nib.