Now that you have installed the SDK, you are ready to start developing for the Iphone application. This tutorial enables you to user the various tools quickly without getting bogged down with the details. It also provides you with instant gratification: You see yourself that things really work, which can be morale booster that inspires you to learn more.

Getting Started with Xcode:

Power up Xcode and you should see the Welcome screen


To create a new iPhone project, choose File ➪ New Project. The different types of projects you can create using Xcode. The left panel shows the two primary categories — iPhone OS and Mac OS X. The iPhone uses the iPhone OS , so click the Application item listed under iPhone OS to view the different templates available for developing your iPhone application.

Name the project HelloWorld and click Save. Xcode proceeds to create the project for the template you have selected. Figure 2-3 shows the various fi les and folders automatically created for your project.



Interface Builder:


At this point, the project has no UI. To prove this, simply press Command-R (or select Run ➪ Run), and your application is deployed to the included iPhone 4 Simulator. The blank screen displayed on the iPhone Simulator. It’s good to see this now, because as you go through the chapter you will see changes occur based on your actions.


In the list of fi les in your project, you’ll notice two fi les with the .xib extension MainWindow.xib and HelloWorldViewController.xib. Files with .xib extensions are basically XML fi les containing the UI defi nitions of an application. You can edit .xib fi les by either modifying their XML content or, more easily (and more sanely), editing them using Interface Builder.

Double-click the HelloWorldViewController.xib fi le to launch Interface Builder..



After the Label view is added, select it and choose Tools ➪ Attributes Inspector. Enter Hello World! in the Text field . Then, next to Layout, click the center Alignment type.



With the Label view still selected, press Command-T to invoke the Fonts window. Set the font size to 36


Next, from the Library window, drag and drop a Text Field view to the View window, followed by a Round Rect Button view. Modify the attribute of the Round Rect Button view by entering Click Me! in the Title field.


Save the HelloWorldViewController.xib fi le by pressing Command-S. Then, return to Xcode and run the application again by pressing Command-R. The iPhone 4 Simulator now displays the modified UI.


Writing Some code:

By now you should be comfortable enough with Xcode and Interface Builder to write some code. This section will give you a taste of programming the iPhone.

In the HelloWorldViewController.h file, add a declaration for the btnClicked: action:
#import <UIKit/UIKit.h>
@interface HelloWorldViewController : UIViewController 
{
}
-(IBAction) btnClicked:(id) sender;
@end

The bold statement creates an action (commonly known as an event handler) named btnClicked:.

In the HelloWorldViewController.m file, add the code that provides the implementation for the btnClicked: action:

#import “HelloWorldViewController.h”
@implementation HelloWorldViewController
-(IBAction) btnClicked:(id) sender {
//--display an alert view--
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:@”Hello World!”
message:@”iPhone, here I come!”
delegate:self
cancelButtonTitle:@”OK”
otherButtonTitles:nil];
[alert show];
[alert release];
}

The preceding code displays an alert containing the sentence “iPhone, here I come!”
That’s it! Go back to Xcode and run the application again. This time, when you click the Button view, an Alert view displays


Download Code

Categories:

Leave a Reply