This tutorial is meant to be a begginners guide to using the iPhone SDK. First off, you will need an Intel based Mac 0S 10.5, and the iPhone SDK.
1. Open XCode which is under HD -> Developer -> Applications.
2. Click on File -> New Project -> select iPhone OS - Application on the left -> View Based Application -> Choose -> Name project "Fields and Buttons"
3. Open "Fields_and_ButtonsViewController.h" and make it look like below:
#import <UIKit/UIKit.h>
@interface Fields_and_ButtonsViewController : UIViewController {
IBOutlet UITextField *field;
IBOutlet UIButton *button;
}
- (IBAction)buttonWasClicked; // Method to be called when button is clicked
@property (nonatomic, retain) IBOutlet UITextField *field;
@property (nonatomic, retain) IBOutlet UIButton *button;
@end
4. Open "Fields_and_ButtonsViewController.m" and make it look like below:
// ---- Begin Code --- //
#import "Fields_and_ButtonsViewController.h"
@implementation Fields_and_ButtonsViewController
@synthesize field, button;
// This method is called when the button is clicked
- (IBAction)buttonWasClicked {
field.text = @"The button was clicked";
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
// Is only called with the iPhone runs low on memory. Don't consider for now.
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
// Free up the memory used
- (void)dealloc {
// free up the memory used by the field and button
[field release];
[button release];
// dealloc memory used by the inherite class
[super dealloc];
}
@end
// ---- End Code ---- //
5. Double click on "Fields_and_ButtonsViewController.xib"
6. Click on the "Tools" menu, and then "Inspector". Click again on the "Tools" menu and then "Library" -> "Objects" -> "Inputs and Values"
7. Select and drag a "Text Field" to the gray View window. In the Inspector window place "this is the field" as the "Placeholder".
8. Do the same for a "Rounded Rect Button", but use "Click Me" as the Title.
9. Click on "Files Owner" and then on the menu bar click on "Tools" and then "Connections Inspector"
10. Under Outlets, click and drag on the circle to the right of "button", on the button you just added in step 8 in the "View" window.
11. Under Outlets, click and drag on the circle to the right of "field", on the text field you added in step 7.
12. Under "Received Actions", drag the circle to the right of "buttonWasClicked" onto the "Click Me" button, a menu will pop up, select "Touch Up Inside"
13. Save and Quit
14. Go back to XCode and save all changes.
15. In XCode go to the "Build" menu and then under "Build and Go (Run)".
As you click on the "Click Me" button you will see the text "The button was clicked" appear in the textfield.
MeasureIt