HelloWorld

Modified

Disclosure

Example from The iPhone Developer's Cookbook by Erica Sadun, Addison Wesley ISBN-13 978-0-321-55545-8

Downloads

HelloWorld application

Button application

Overview

Generally, using the interface development tools (Interface Builder) and the Xcode bells and whistles save time and effort, when the solution fits the tools.

First an Xcode and Interface Builder HelloWorld1 application.

But to understand what happens behind the development tool curtain, one needs to see the raw details of the implementation.

HelloWorld1

Basic iOS applications minimally consist of 4 parts:

For the HelloWorld1 application the parts are:

  1. main - called first to start the application (in argv): UIApplicationMain(argc, argv, nil, nil);
  2. HelloWorld1AppDelegate - constructs window and subview for displaying title and visible objects.
  3. HelloWorld1ViewController - where the images, buttons, etc. are displayed, the Controller.
  4. HelloWorld1ViewController NIB file - the View, constructed using Interface Builder.

iOS apps are made up of views, displayed by a view controller such as:

@interface HelloWorld1ViewController : UIViewController

Minimally, Xcode requires editing two text files (listed below) and the application nib file to add UI objects and connections.

The added elements are in bold.

Xcode at IUS

Open Finder    

Navigate to the Xcode application.

Double click to start Xcode.

 

 

 

 

 

 

 

 

 

 

 

 

 

iOS Project Creation

A basic iOS application is created by opening Xcode and selecting:

  1. File | New Project | iOS Application | View-based Application
     
  2. Save As: HelloWorld1
     
  3. Expand Classes
     
  4. Click HelloWorld1ViewController.h
     
  5. Add the bold code below.
    HelloWorld1ViewController.h
    #import <UIKit/UIKit.h>
    
    @interface HelloWorld1ViewController : UIViewController {
    	IBOutlet UILabel *labelHelloWorld;
    }
    -(IBAction) helloWorld: (id) sender; 
    @end
  6. Click HelloWorld1ViewController.m
     
  7. Add the bold code below.
     
  8. Build | Build

    If no errors can now construct the GUI.

    HelloWorld1ViewController.m
    #import "HelloWorld1ViewController.h"
    
    @implementation HelloWorld1ViewController
    
    -(IBAction) helloWorld: (id) sender {
    	[labelHelloWorld setText: @"Hello World!"];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }
    - (void)dealloc {
        [super dealloc];
    }
    @end
    

IB - Interface Builder

  1. Expand NIB Files
     
  2. Click HelloWorld1ViewController.xib to open IB to the default and empty view created.
     
  3. Open Tools | Library
     
  4. Drag a UIButton and UILabel onto the View
     
  5. Click the button and edit the text to Click.

Below left is the View after an UIButton and UILabel were dragged from the Library to the View.

Connecting GUI objects to code

IB can connect the GUI objects to Objective C definitions:

IBOutlet UILabel labelHelloWorld is connected to the View Label by:

  1. Right click (press control key) Label object,
  2. New Referencing Outlet, click O and drag to File Owner
  3. Select labelHelloWorld

IBAction method is connected by:

  1. Right click (press control key) Click button
  2. Touch Up Inside, click O and drag to File Owner
  3. Select helloWorld:

Execute

  1. Save file.
  2. Build and Debug in Xcode.

 

HelloWorld

The following examines a hand-written iPhone application, the sacrosanct first program, HelloWorld.

iOS applications typically consist of 3 parts, all of which can be in a single file.

For the HelloWorld application the parts are:

  1. main - called first to start the application. Launches: UIApplicationMain(argc, argv, nil, @"HelloWorldAppDelegate");
  2. HelloWorldAppDelegate - constructs window and subview for displaying title and visible objects.
  3. HelloController - where the images, buttons, etc. are displayed.

iOS apps are made up of views, displayed by a view controller such as:

@interface HelloController : UIViewController

in which a view is constructed, contentView, and assigned as the view of the view controller by:

self.view = contentView;

Note, no memory management for a simpler example.

#import <UIKit/UIKit.h>

int main(int argc, char *argv[]) {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   int retVal = UIApplicationMain(argc, argv, nil, @"HelloWorldAppDelegate");
   [pool release];
   return retVal;
}
@interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate> {}
@end

@implementation HelloWorldAppDelegate                 // On launch, create a basic window

- (void) applicationDidFinishLaunching:(UIApplication *) application {
   UIWindow *window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];

   UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:
                                                    [HelloController new]];

   [window addSubview: nav.view];
   [window makeKeyAndVisible];
}
@end
@class UIImageView;

@interface HelloController : UIViewController {}
@end

@implementation HelloController
- (id) init {
   [super init];
   self.title = @"Hello World!!";
   return self;
}

- (void) loadView {              // Load an application image and set it as the primary view
   UIImageView *contentView = [[UIImageView alloc] initWithFrame:
                           [[UIScreen mainScreen] applicationFrame]];

   [contentView setImage:[UIImage imageNamed:@"helloworld.png"]];

   self.view = contentView;  // Assign the view to the view controller
   [contentView release];
}
@end

 

Button

#import <UIKit/UIKit.h>

@class UIImageView;

@interface ButtonAppDelegate : NSObject <UIApplicationDelegate> {}
@end
@implementation ButtonAppDelegate
									// On launch, create a basic window
- (void) applicationDidFinishLaunching:(UIApplication *)application {	
	UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

	UINavigationController *nav = 
       		[[UINavigationController alloc] initWithRootViewController:[ButtonController new]];

	[window addSubview:nav.view];
	[window makeKeyAndVisible];
}
@end
@interface ButtonController : UIViewController {
	UIButton *button;
}
@end
@implementation ButtonController
- (id) init {
	[super init];
	self.title = @"Hello Button!!";
	return self;
}

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	
	self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	
	self.view.backgroundColor = [UIColor whiteColor];
	
	button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	
	button.frame = CGRectMake(100, 170, 100, 30);
	
	[button setTitle:@"Click Me!" forState:UIControlStateNormal];
	
	//listen for clicks
	[button addTarget: self 
   		   action:@selector(buttonPressed) 
		   forControlEvents:UIControlEventTouchUpInside];
	
	//add the button to the view
	[self.view addSubview:button];
}

-(void)buttonPressed {
	[button setTitle:@"Again!" forState:UIControlStateNormal];
}
@end
int main(int argc, char *argv[]) {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   int retVal = UIApplicationMain(argc, argv, nil, @"ButtonAppDelegate");
   [pool release];
   return retVal;
}