iOS HelloWorld example

Horray, you’ve got your mac and your iPhone, and you want to write some code and see how it goes. You can find a lot of Hello World tutorials around the web, but I believe that 95% or those tutorials use Interface Builder, and I’m not a big fan of IB, because it does some “magic” behind the scenes, and IMO it’s really nice to know everything that’s happening within your app. IB is not useless, but let’s not enter on this topic (yet). I think that’s better to write your first app without IB so you can see what’s really going on.

If you haven’t installed Xcode yet, go to http://developer.apple.com/iphone/index.action and get it. This dmg will install everything that you need, iPhone SDK, iPhone Simulator, XCode, and a bunch of useful apps, like Instruments and so on. By default, Xcode will be installed on /Developer/Applications/Xcode.app, go there and open it (or just hit command + space and type Xcode on your spotlight).

Go ahead and create a new Xcode project. You’ll be prompt to choose some project templates, we can select View-Based Application, it has a ViewController already configured, but let’s choose the Window-Based Application. The Window-Based application is the simplest template that you can think of, it will give you a Windows and an UIApplicationDelegate that responds to UIApplication. These files are required in any iOS application, so, that’s why the Window-Based application is as simple as it gets.

Name the project Hello World and this is what you’ll get :

The template creates a lot of required files for you, but let’s focus on the HelloWorldAppDelegate.h and HelloWorldAppDelegate.m, but take a look in the main.m file, you can see that the UIApplication is being created there.

Open the HelloWorldAppDelegate.m file, and check the following method:

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

[window makeKeyAndVisible];

return YES;

}

This method gets called when the initial configuration of the app is done, and your app is ready to launch, so it’s there that you have to call your initials methods, like in this example, our UIViewController. You have to be careful in what kind of method you call in there, because if your app takes a while to respond it will be watchdogged. For example, if you call some method that requires a server response and the server does not responds back for a while, the iOS will kill your app, but let’s forget about this for now.

We could just add the label directly to the window, like so :

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

CGRect labelFrame = CGRectMake(50, 50, 200, 30);

UILabel *helloLabel = [[UILabel alloc] initWithFrame: labelFrame];

helloLabel.text = @”Hello World”;

[window addSubview:helloLabel];

[window makeKeyAndVisible];

[helloLabel release];

return YES;

}

IT’S DONE! Tutorial finished!

OK, sorry, that’s not true, I mean, you can do this, but you don’t want to do like this, believe me 😉

Let’s do it right, forget about the last piece of code, and let’s start again. Let’s create an UIViewController subclass, and add it to the window. It’s on the view controller that you will add your label, and whatever you need. Go to File -> New File (or command + N) and create an UIViewController subclass.

Let’s name it HelloWorldViewController like so:

Open the HelloWorldViewController.m, as you can see, you have the basic file structure all ready for your use (Hey, thank you, Apple guys). Search for the method named – (void)loadView, which is used to manually populate a view controller.

Let’s copy the same code that we use previously in the appDelegate

– (void)loadView {

UIView *mainView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];

CGRect labelFrame = CGRectMake(50, 50, 200, 30); 

UILabel *helloLabel = [[UILabel alloc] initWithFrame: labelFrame];

helloLabel.text = @”Hello World”;

[mainView addSubview:helloLabel];

[helloLabel release];

self.view = mainView;

[mainView release];
 

}

Let’s see what are we doing here :

  1. UIView *mainView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds; Creating our UIView with the same size of the main screen.
  2. CGRect labelFrame = CGRectMake(50, 50, 200, 30); The CGRect will be the bounds for your view.
  3. UILabel *helloLabel = [[UILabel alloc] initWithFrame: labelFrame]; This will create the UILabel class with the CGRect that we have created.
  4. helloLabel.text = @”Hello World”; Just set the string on the label text. Why add an @ at the beginning of the string? That’s to differentiate the NSString from the C String.
  5. [self.view addSubview:helloLabel]; Every UIViewController controls  a UIView (yey, it does makes sense, doesn’t it?), so, to access it you just have to use self.view and that’s it. Here we are adding the helloLabel (that is a UIView subclass) to the HelloWorldViewController UIView
  6. [helloLabel release]; Since we have added the helloLabel to the HelloWorldViewController UIView, we can release it, because the UIViewController will handle the memory management for the UILabel
  7. self.view = mainView; Adding out custom UIView to the UIViewController’s UIView.
  8. [mainView release]; The UIViewController now retains the reference to it’s UIView, so, we can release it.

We are almost done, just one more thing: we need to hook up our brand new UIViewController to the HelloWorldAppDelegate  and add it to our window.

To do it, go back to the HelloWorldAppDelegate.m and import our view controller class:

#import “HelloWorldViewController.h”

now, let’s create alloc and init our HelloWorldViewController:

HelloWorldViewController *helloController = [[HelloWorldViewController alloc] init];

add it to the window:

[window addSubview:helloController.view];

and release the HelloWorldViewController. Generally you will not want to release the view controller just yet, but in this example, there’s no problem:

[helloController release];

You will end up with something like this:

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

HelloWorldViewController *helloController = [[HelloWorldViewController alloc] init];

[window addSubview:helloController.view];

[helloController release];

[window makeKeyAndVisible];

return YES;

}

Build and run your project (command + enter), and your should see this :

Pretty simple, huh? Click here to download the HelloWorld example 🙂

Advertisements

7 thoughts on “iOS HelloWorld example

  1. Good tutorial. But I think, what using “autorelease” is better. So, you don’t need to remember about manual releasing objects.

    HelloWorldViewController *helloController = [[[HelloWorldViewController alloc] init] autorelease];

    for example.

    • Ango, thanks for your comment, but I didn’t use the autorelease here because I know exactly when the label is not needed anymore, so I don’t have to wait the pool to release it. it’s better to avoid autoreleased objects when you know it’s exact cycle of life.

  2. Obviously, the less memory you use, the better you are doing.

    The reason to use release instead of autorelease is that autoreleased objects hang around for a while in memory and released objects go right away if no one else is retaining it.

    Therefore, using release instead of autorelease can improve memory usage in tight spots (which is good for mobile devices).

    • Perhaps you were inspired by WWDC 2009 seoissn 128 (prototyping for iPhone) I’ve been looking for that particular code for a while with no luck, and the presenter didn’t scroll through the whole .m file in his demo. However, what you put together works perfectly and I was able to go through the whole prototyping process easily because of this post thanks!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s