Simple Calculator

This tutorial shows how to create a simple calculator with basic arithmetic functions. For simplicity sake, the calculator can only handle integers. .

Update: June 30, 2014. This tutorial is made with Xcode 4 and targeted for iOS 6. The rewritten version in Swift for iOS8 and Xcode 6 is available here.

Create a new Project with the "Single View Application" template.

Fill in the following Project Options

Navigate to the iOSViewController.xib file. Drag a label to the top of the View. In the Attribute inspector, select the right alignment. Give the Label Field an initial value of 0.

Ctrl+Click and Drag from the Label to the @interface section of iOSViewController.h and create the IBOutlet calculatorDisplay

Drag a button onto the View and Give the button a value of 1.

Ctrl+Click and drag from the button to the @interface section of iOSViewController.h and create the IBAction numberPressed

Copy the number button and paste to the view to create buttons 2-9 and the zero digit. The view should look like this.

Drag a new UIButton to the view. Give the button the plus sign.

Ctrl+Click and Drag from the plus-button to the @interface section of iOSViewController.h and create the IBAction calculationPressed

Copy the plus-button and paste a new button beneath this button. Give the new button a minus sign.

Drag a new UIButton to the view. Give the button the equal sign.

Ctrl+Click and drag from the equal-button to the @interface section of iOSViewController.h and create the IBAction equalsPressed

Create the following properties in iOSViewController.h.

@property (nonatomic) BOOL typingNumber; //check if user is typing a number 
@property (nonatomic) int firstNumber;
@property (nonatomic) int secondNumber;
@property (nonatomic, copy) NSString *operation; // plus or minus operation

Implement the functions numberPressed,calculationPressed and equalsPressed in iOSViewController.m

 

- (IBAction)numberPressed:(UIButton *)sender 
{
NSString *number = sender.currentTitle;
if (self.typingNumber)
{
self.calculatorDisplay.text = [self.calculatorDisplay.text stringByAppendingString:number];
} else
{
self.calculatorDisplay.text = number;
self.typingNumber = YES;
}
}

- (IBAction)calculationPressed:(id)sender
{
self.typingNumber = NO;
self.firstNumber = [self.calculatorDisplay.text intValue];
self.operation = [sender currentTitle];
}

- (IBAction)equalsPressed
{
self.typingNumber = NO;
self.secondNumber = [self.calculatorDisplay.text intValue];

int result = 0;

if ([self.operation isEqualToString:@"+"])
{
result = self.firstNumber + self.secondNumber;
}
else if ([self.operation isEqualToString:@"-"])
{
result = self.firstNumber - self.secondNumber;
}

self.calculatorDisplay.text = [NSString stringWithFormat:@"%d", result];
}

Build and Run the application. As you can see the calculator will perform the basic calculations.

calculator_simulator
calculator_simulator

You can download the source code of the SimpleCalculator at the ioscreator repository on github.