Online Book Reader

Home Category

iPhone Game Development - Chris Craft [23]

By Root 1578 0
Types. Click the add class outlet button (the small plus sign), add a new outlet named helloWorldLabel, and set its Type to UILabel.

Now the Main View class has an outlet named helloWorldLabel that can accept a UILabel control. But what if you had multiple UILabel controls on your form? How would the Main View class know which UILabel it should use for the helloWorldLabel outlet? Actually, right now it doesn't, even with only one UILabel. Basically you have a box that a UILabel control can fit into, and now you need to assign a UILabel control to it. To do this, right-click the label control and choose New Referencing Outlet. Click the little circle on the left of New Referencing Outlet and drag it to the title bar of the Main View's window. You should now see helloWorldLabel and Main View listed in the displayed menu, as shown in Figure 2.22.

FIGURE 2.22

Adding a label outlet to a Main View


Generating code

Interface Builder generates the matching classes and code that you will need in Xcode for the user interface you have created thus far. On the Interface Builder, choose File⇒Write Class Files. A Save dialog box should appear and you should see Save As: MainView selected, as shown in Figure 2.23. If not, make sure you have the Main View window selected in Interface Builder and try again. Once you have the correct settings in the Save dialog box, click the Save button.

FIGURE 2.23

Save dialog box


You are asked if you want to replace the existing files with the new files Interface Builder has generated (Figure 2.24). You can safely click Replace, since you do not have any code you have hand-written at this point. If you did have existing code of your own, you could click Merge to merge the two code sets. For the most part, Xcode can merge code safely, but it can be a little too advanced for new developers first starting out, so it is best to avoid this option for now. (The third option, Cancel, is available in case you decide you are not ready to generate code.)

FIGURE 2.24

Replace or Merge prompt


A lot of the heavy lifting is done now. You have all of your graphics and text controls placed and ready. If you like, you can click the Build and Go button in the main toolbar of the Xcode Project Explorer, or simply press Ô+R. Either option will cause Xcode to launch your Hello World application in the iPhone Simulator.

Coding the application

You're not quite done. You still need to write a little code to make the real magic happen. Go back into Xcode and choose Groups & Files⇒HelloWorldEx⇒Main View⇒MainView.m. Type the following code to make your MainView.m match the following listing:

#import “MainView.h”

@implementation MainView

-(void)awakeFromNib {

helloWorldLabel.text = “Hello, World!”;

}

@end

The awakeFromNib method is an event that is by design automatically called when a view is first being loaded. You can try changing the helloWorldLabel text and then use Build and Go to see the results in the iPhone Simulator. Even better, you can write some code to do it for you. We are going to use an NSTimer object to update the helloWorldLabel every three seconds. Update your MainView.m file to look like the following:

#import “MainView.h”

@implementation MainView

static int timerCount = 0;

- (void)awakeFromNib {

helloWorldLabel.text = “Hello, World!”;

[NSTimer scheduledTimerWithTimeInterval(3.0) target:self selector:@selector(onTimer)

userInfo: nil repeats:YES];

}

- (void)onTimer {

timerCount++;

switch (timerCount % 5) {

case 0:

// say “Hello, World!” in English

helloWorldLabel.text = @”Hello, World!”;

break;

case 1:

// say “Hello, World!” in French

helloWorldLabel.text = @”Bonjour, monde!”;

break;

case 2:

// say “Hello, World!” in Spanish

helloWorldLabel.text = @”Hola, Mundo!”;

break;

case 3:

// say “Hello, World!” in German

helloWorldLabel.text = @”Hallo, Welt!”;

break;

case 4:

// say “Hello, World!” in Italian

helloWorldLabel.text = @”Ciao, Mondo!”;

break;

}

}

@end

You now have a fairly

Return Main Page Previous Page Next Page

®Online Book Reader