Skip to content

Instantly share code, notes, and snippets.

@alonsorobles
Created April 17, 2012 00:59
Show Gist options
  • Select an option

  • Save alonsorobles/2402660 to your computer and use it in GitHub Desktop.

Select an option

Save alonsorobles/2402660 to your computer and use it in GitHub Desktop.
#import "CalculatorViewController.h"
#import "Calculator.h"
@interface CalculatorViewController ()
@property (nonatomic) BOOL userIsEnteringANumber;
@property (nonatomic, strong) Calculator *calculator;
@end
@implementation CalculatorViewController
@synthesize display = _display;
@synthesize userIsEnteringANumber = _userIsEnteringANumber;
@synthesize calculator = _calculator;
- (Calculator *) calculator {
if (!_calculator) _calculator = [[Calculator alloc] init];
return _calculator;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setDisplay:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)digitPressed:(UIButton *)sender {
NSString *digit = sender.currentTitle;
if (self.userIsEnteringANumber)
self.display.text = [self.display.text stringByAppendingString:digit];
else if (digit != @"0") {
self.display.text = digit;
self.userIsEnteringANumber = YES;
}
}
- (IBAction)operandPressed:(UIButton *)sender {
if (self.userIsEnteringANumber) [self enterPressed];
double result = [self.calculator perform:sender.currentTitle];
self.display.text = [NSString stringWithFormat:@"%g", result];
}
- (IBAction)enterPressed {
[self.calculator push:[self.display.text doubleValue]];
self.userIsEnteringANumber = NO;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment