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:
- main - called first to start the application (in argv): UIApplicationMain(argc, argv, nil, nil);
- HelloWorld1AppDelegate - constructs window and subview for displaying title and visible objects.
- HelloWorld1ViewController - where the images, buttons, etc. are displayed, the Controller.
- 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:
- File | New Project | iOS Application | View-based Application
- Save As: HelloWorld1
- Expand Classes
- Click HelloWorld1ViewController.h
- Add the bold code below.
HelloWorld1ViewController.h #import <UIKit/UIKit.h> @interface HelloWorld1ViewController : UIViewController { IBOutlet UILabel *labelHelloWorld; } -(IBAction) helloWorld: (id) sender; @end- Click HelloWorld1ViewController.m
- Add the bold code below.
- 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
- Expand NIB Files
- Click HelloWorld1ViewController.xib to open IB to the default and empty view created.
- Open Tools | Library
- Drag a UIButton and UILabel onto the View
- 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 objects such as UILabel
- IBAction methods such as -(IBAction) helloWorld: (id) sender
IBOutlet UILabel labelHelloWorld is connected to the View Label by:
- Right click (press control key) Label object,
- New Referencing Outlet, click O and drag to File Owner
- Select labelHelloWorld
IBAction method is connected by:
- Right click (press control key) Click button
- Touch Up Inside, click O and drag to File Owner
- Select helloWorld:
Execute
- Save file.
- 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:
- main - called first to start the application. Launches: UIApplicationMain(argc, argv, nil, @"HelloWorldAppDelegate");
- HelloWorldAppDelegate - constructs window and subview for displaying title and visible objects.
- 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 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; } |