Due to the recent announcement of the iPad more people are interested to learn how to develop for the iPhone OS. This tutorial will be a basic learning tutorial for beginners who want to start developing iPad applications.The application that we will be creating will consist of two button and one label.
Create a new project
First of all open Xcode and click "Create a new Xcode project". Select View-based Application and then iPad as the product.Save the project as Tutorial.
Outlets and Actions
For things such as labels and textfields we use IBOutlets and for buttons we use IBActions. IB stands for Interface Builder and is what we will use to create our interface and link the connections.To add our outlets and actions open up TutorialViewController.h.
Under @interface TutorialViewController : UIViewController { add:
IBOutlet UILabel *currentNumber;
-(IBAction)incrementNumber:(id)sender; -(IBAction)decrementNumber:(id)sender;
Create the interface and make connections
Open up TutorialViewController.xib. From the library drag one labels and two buttons.Control + click the File's Owner object and drag it to the label then select currentNumber; now go from the minus and plus button to the File's Owner object and select decrementNumber for the minus button and incrementNumber for the plus button.
Save TutorialViewController.xib and close Interface Builder.
The Code
Open TutorialViewController.m. Below the implementation we need to create an integer called number and set the value to 0.int number = 0;
When the increment button is pressed we will increment the number integer by 1 then set the value of currentNumber to number by using a formatted string.
-(IBAction)incrementNumber:(id)sender { number++; [currentNumber setText:[NSString stringWithFormat:@"%d", number]]; }
-(IBAction)decrementNumber:(id)sender { number-; [currentNumber setText:[NSString stringWithFormat:@"%d", number]]; }
No comments:
Post a Comment