iOS Dev – Assignment #2 – Task #4

Delegation The BasicCalculator communicates its result to a delegate. This asynchronous implementation is a bit overhead in this simple example, but think of a scientific calculator with more elaborate functionality. You can start a very complex operation and the Calculator-Class takes care of processing this operation in the background. After some time it finishes and communicates its result to the controller. Have a close look at the implementation of the delegation in the Basic- Calculator class. Now make your ViewController class the delegate of the calculator and make the project a fully working calculator.

When the view has loaded initiate the calculator and set the view controller as delegate of the calculator:

- (void)viewDidLoad
{
    ...    
    self.calculator = [[BasicCalculator alloc] init];
    self.calculator.delegate = self;
}

Instead of using the calculation method from the view controller call the calculator functions:

- (IBAction)operationButtonPressed:(UIButton *)sender {
    if ([self.calculator.lastOperand isEqualToNumber:@0]) {
        [self.calculator setFirstOperand:
            [NSNumber numberWithFloat:
                [self.numberTextField.text floatValue]]];
        currentOperation = sender.tag;
    } else {
        [self.calculator performOperation:currentOperation 
                              withOperand:[NSNumber numberWithFloat:
                                  [self.numberTextField.text floatValue]]];
        currentOperation = sender.tag;
    }
    textFieldShouldBeCleared = YES;
}

- (IBAction)resultButtonPressed:(id)sender {
    [self.calculator performOperation:currentOperation 
                          withOperand:[NSNumber numberWithFloat:
                              [self.numberTextField.text floatValue]]];
    currentOperation = BCOperatorNoOperation;
    [self.calculator reset];
    textFieldShouldBeCleared = YES;
}

- (IBAction)clearDisplay:(id)sender {
    [self.calculator reset];
    currentOperation = BCOperatorNoOperation;
    self.numberTextField.text = @"0";
    textFieldShouldBeCleared = YES;
}

… of course there are much better ways to get an NSNumber from an NSString … but for now we just reuse as much of the “old” code as possible.

Finally add the missing operations to the calculator class:

- (void)performOperation:(BCOperator)operation withOperand:(NSNumber*)operand;
{
    NSNumber *result;
    if (operation == BCOperatorAddition) {
        result = [NSNumber numberWithFloat:
            ([self.lastOperand floatValue] + [operand floatValue])];
        self.lastOperand = result;
    } else if (operation == BCOperatorSubtraction) {
        result = [NSNumber numberWithFloat:
            ([self.lastOperand floatValue] - [operand floatValue])];
        self.lastOperand = result;
    } else if (operation == BCOperatorMultiplication) {
        result = [NSNumber numberWithFloat:
            ([self.lastOperand floatValue] * [operand floatValue])];
        self.lastOperand = result;
    } else if (operation == BCOperatorDivision) {
        result = [NSNumber numberWithFloat:
            ([self.lastOperand floatValue] / [operand floatValue])];
        self.lastOperand = result;
    }
    ...
}
FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published.