Hello World in Xcode 4.0

May 17, 2011

We will start off by creating a new project and selecting a Window-based Application.

And naming it HelloWorld

Next we want to create a view controller. Go to File > New > New File... and select UIViewController subclass.

A UIViewController subclass works by overriding functions in its base class. The function of particular interest to us is the loadView. This is where we will add controls to our view.

- (void)viewDidLoad
    //Create a frame
    CGRect frame = CGRectMake(0, 0, 320, 480);
    //Initialize the view with our frame
    self.view = [[UIView alloc] initWithFrame:frame];
    //Set the background color of the view to be white
    self.view.backgroundColor = [UIColor whiteColor];
    [super viewDidLoad];

But before we add controles to the view, we need to create a frame which will hold the view. CGRect frame = CGRectMake(0, 0, 320, 480); creates a frame starting at point (0, 0) (the upper left corner) with a width of 320 pixels by 480 pixels (the size of an iPhone screen). We then allocate and initialize the view with the frame we just created. Now that we have a view, we can add our label.

- (void)viewDidLoad
    CGRect frame = CGRectMake(0, 0, 320, 480);
    self.view = [[UIView alloc] initWithFrame:frame];
    self.view.backgroundColor = [UIColor whiteColor];
    //Create a frame to hold the label
    frame = CGRectMake(50, 50, 100, 50);
    //Create and initialize the label
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    //Set the labels text to Hello, World!
    label.text = @"Hello, World!";
    //Add the label to the view
    [self.view addSubview:label];
    //Release the reference
    [label release];
    [super viewDidLoad];

Like before, we start by creating a frame to hold the label. Once the frame is created, we can allocate a UILabel, initialize it, set its text, and add it to the view. Lastly, since Objective-C uses reference counting, it is a good idea to release the label to avoid any memory leaks - as a general rule of thumb, if you use a * make sure you release it.

Now that we have a view controller with a label, we need to add the view controller to the window. In your HelloWorldAppDelegate.h start by forward declaring the HelloWorldViewController class - this will tell the app delegate the HelloWorldViewController class exists. Then create an instance variable of type HelloWorldViewController, and finally make it a property.

#import <UIKit/UIKit.h>
//Forward declare the class
@class HelloWorldViewController;
@interface HelloWorldAppDelegate : NSObject  {
    //Create an instance variable (don't forget to release this)
    HelloWorldViewController *viewController;
@property (nonatomic, retain) IBOutlet UIWindow *window;
//Create a property
@property (nonatomic, retain) HelloWorldViewController *viewController;

Finally, in HelloWorldAppDelegate.m import the HelloWorldViewController.h file.

#import "HelloWorldAppDelegate.h"
#import "HelloWorldViewController.h"

Synthesize the viewController instance variable

@synthesize window=_window;
@synthesize viewController;

Then finally, allocate a HelloWorldViewController and add to the window viewController.view as a subview.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    self.viewController = [HelloWorldViewController alloc];
    [self.window addSubview:self.viewController.view];
    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];
    return YES;

Be sure to release viewController when you are done with it!

- (void)dealloc
    [viewController release];
    [_window release];
    [super dealloc];

Build and Go!