Please note, this blog entry is from a previous course. You might want to check out the current one.
Add an Undo button to your calculator. Hitting Undo when the user is in the middle of typing should take back the last digit or decimal point pressed until doing so would clear the display entirely at which point it should show the result of running the brain’s current program in the display (and now the user is clearly not in the middle of typing, so take care of that). Hitting Undo when the user is not in the middle of typing should remove the top item from the program stack in the brain and update the user- interface.
This task can be implemented with 1 method in your Controller (of about 5-6 lines of code, assuming you’ve factored out the updating of your user-interface into a single method somewhere) and 1 method (with 1 line of code) in your Model. If it’s taking much more than that, you might want to reconsider your approach.
The first part of this task has already been implemented in the previous assignment. So we have to adjust the backSpace method to achieve the rest. To receive the result of the last program when the last entry is cleared we just add
[self updateCalculatorView];
after we set userIsInTheMiddleOfEnteringANumber to NO. And for the functionality of removing the last item on stack we add
if (!self.userIsInTheMiddleOfEnteringANumber) { [self.brain clearLastItem]; [self updateCalculatorView]; return; }
to the controller and add the new API we just used to the model
- (void) clearLastItem { [self.programStack removeLastObject]; }