In this article, I implement my very first iPhone application starting from View-Based Application, which is a template for applications that uses a single view to implement its user interface.
Create a new Project in Xcode, select View-Based Application from Application under iPhone OS, choose a location and Save As helloiphone.
In the project window, expand the Classes folder under helloiphone. There are four files generated:
- helloiphoneAppDelegate.h
- helloiphoneAppDelegate.m
- helloiphoneViewController.h
- helloiphoneViewController.m
We will deal on the four files mainly.
ViewController -
There is only one view in the application. helloiphoneViewController, which is a subclass of UIViewController, is a controller class that manage the view. UIViewController is a part of UIKit.
AppDelegate -
The other two files implement the application delegate, classes response to do something at certain times on behalf of UIApplication. In certain times, UIApplication will call the implemented delegate methods, such as applicationDidFinishLaunching. In this application, we have no need to touch the AppDelegate files.
IBOutlet -
The controller class refer to a object (such as a text label) in nib using outlet, such that the code can change the property (such as the text of the label) of a object using outlet.
IBAction -
Interface object (such as a button) can generate action methods (such as touch up) in the controller class
We are going to implement a action method, named buttonPressed, which will be triggered by the interface object button. And create an outlet point to the text label. The code inside the action method change the text of the text label using the out.
Modify the file helloiphoneViewController.h
#import <UIKit/UIKit.h>
@interface helloiphoneViewController : UIViewController {
UILabel *labelText;
}
@property (nonatomic, retain) IBOutlet UILabel *labelText;
- (IBAction)buttonPressed:(id)sender;
@end
Modify the file helloiphoneViewController.m
#import "helloiphoneViewController.h"
@implementation helloiphoneViewController
@synthesize labelText;
-(IBAction)buttonPressed:(id)sender{
NSString *newText = [[NSString alloc] initWithFormat:@"Hello iPhone!"];
labelText.text = newText;
[newText release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[labelText release];
[super dealloc];
}
@end
Create the View using Interface Builder
Expand the Resources, and double click to open helloiphoneViewController.xib in Interface Builder.
Drag a Label from Library to View, place on any where you want, double click on it to edit the starting text, eg. Hello!.
Drag a Round Rect Button from Library to View, place on any where you want, double click on it to edit the starting text, eg. Press Me.
Connect the Outlet
Hold down the Control key and drag (or drag by right button on mouse) the File's Owner in helloiphoneViewController.xib to over the Label in View. Release and select labelText.
Connect the Action
Select Tools from top menu of Interface Builder, select Connections Inspector. Click on the Button on the View. The Connections Inspector will show the available events of the Button.
Drag the circle on the right of the event Touch Up Inside, to over File's Owner in helloiphoneViewController, release and select buttonPressed.
Save the work and go back to Xcode, click Build and Go.
The Label display Hello! in start-up, and change to Hello iPhone! once the button pressed.










