iPhone SDK Tutorial: A Simple “Hello World” Program.
The iPhone SDK is a great platform for developers. It gives developers a great opportunity to reach a huge market of users easily. So, let’s great started so you can start building your own great apps.
XCode and Objective-C
I am assuming most of you have never worked with Objective-C or XCode before. Neither had I until I wanted to develop iPhone apps. The first thing you should note is that Objective-C has a very different syntax than languages such as C# and Java. Let’s go over some basic differences by comparing the same code in Objective-C and Java.
Objective-C:
PointCountsViewController *aViewController = [[PointCountsViewController alloc] initWithNibName:@”PointCountsViewController” bundle:[NSBundle mainBundle]];
Java:
PointCountsViewController aViewController = new PointCountsViewController(”PointCountsViewController”, mainBundle);
As you can tell when calling an object’s method, instead of doing something like object.method(parameter) in Objective-C you would do: [object method:parameter];
Similar to C++ each file has a header file and a source file. The header file has the extension of .h while the source file has extension of .m. Whenever you create a class you create the interface in the header file and implemented it in the source file.
![]()
Now that we are familiar with some basic concepts of Objective-C, let’s take a look at how XCode is organized.
All of the code files are placed within the Classes folder. Things such as images and the .xib files are placed in the Resources folder. The .app that is produced when you compile is built and placed within the Products folder. You can essentially ignore everything else (at this point).
The .xib file that I mentioned earlier is a file that stores the interface/gui of your application. You can edit it using Interface Builder by simply double clicking it.
Creating the Project
Now that you are familiar with what files you will use and some basic Objective-C syntax, let’s get started and create a new project.
Go to File -> New Project -> iPhone OS -> Application -> View-Based Application
Name your application: HelloWorld and create the project.
Now, let’s open up the Resources folder. Double click on the HelloWorldViewController.nib file. The file should load up in Interface Builder. This is where you can drag and drop items to create your application’s interface. The first thing we want is to add a label. So, let’s get a Label from the Library (if your library isn’t open, go to Tools -> Library to open it) and drag it into the view.
Let’s Get Coding
Now that we have designed the GUI of our application, let’s create the code that prints out hello world on screen touch.
Open up HelloWorldViewController.h and replace the file with the following code:
#import <UIKit/UIKit.h>
@interface HelloWorldViewController : UIViewController {
IBOutlet UILabel *lblText;
}
@property (nonatomic, retain) UILabel *lblText;
@end
What this code does is create an object of UILabel that we will (in a few steps) link up with the label we created in interface builder.
Now let’s add the real code that does all the work. Open up HelloWorldViewController.m and put the following code inside:
@synthesize lblText;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
lblText.text = @”Hello World”;
[super touchesBegan:touches withEvent:event];
}
What this code does is sense when a touch has occurred anywhere on the screen, and when it has we update the label to say “Hello World”.
We are still not done. The label that we created by declaring IBOutlet UILabel lblText is not linked up to the label that we create in Interface Builder. So, now let’s open up interface builder again. Go to the inspector (if it’s not already open, open it from Tools -> Inspector) and click on the tab that has a blue circle with an arrow inside. Now go back to the view and click on your label. Now that it is selected click on the blue circle next to “New Referencing Outlet” in the Inspector and drag it to the File Owner window.
Essentially that’s it. Build and Go and you should see your app in the iPhone simulator. :)

![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=4451d781-a38f-49a4-8383-220193ae9273)

Add New Comment
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Add New Comment