The problem can be simplified by realizing that the numerical value of the input doesn't matter until you must do a calculation. You can let the buttons and the text field just concern themselves with textual behavior, then extract a value any time you need it. e.g.
// do this for digits and @"."
- (IBAction)button9:(id)sender {
[self appendDigit:@"9"];
}
- (void)appendDigit:(NSString *)digit {
// handle two special cases: append to only zero means just replace
// but append decimal point to zero is a regular append
if ([self.myTextField.text isEqualToString:@"0"] && ![digit isEqualToString:@"."]) {
self.myTextField.text = digit;
} else {
self.myTextField.text = [myTextField.text stringByAppendingString:digit];
}
}
// negative sign is special too...
self.myTextField.text = [@"-" stringByAppendingString:myTextField.text];
Then, only later, when you need to do a calculation, you can get the numerical value like this:
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *number = [f numberFromString:self.myTextField.text];
Finally, another nice representational pattern is to keep numerical state as NSNumbers, converting to scalar types when math is needed...
NSNumber *runningTotal;
NSNumber *currentValue;
// say the current operation is multiplication
float runningTotalFloat = [runningTotal floatValue];
float currentValueFloat = [currentValue floatValue];
float newRunningTotal = runningTotalFloat * currentValueFloat;
runningTotal = [NSNumber numberWithFloat: newRunningTotal];