#import "Controller.h"
@implementation Controller
- (void)awakeFromNib
{
[billField becomesFirstResponder];
}
- (IBAction)calculateTip:(id)sender
{
static BOOL toggle = YES;
if (toggle)
{
toggle = NO;
NSString *billFieldText = billField.text;
float newTotal = [billFieldText floatValue];
float customTipPercent = customPercentSlider.value;
if (sender == billField)
{
if (billFieldText.length < billTotal.length)
billTotal = [NSString stringWithFormat:@"%.02f",
newTotal / 10];
else
billTotal = [NSString stringWithFormat:@"%.02f",
newTotal * 10];
billField.text = billTotal;
newTotal = [billTotal floatValue];
float tenTip = newTotal * 0.10;
float fifteenTip = newTotal * 0.15;
float twentyTip = newTotal * 0.20;
tipFieldTen.text = [NSString stringWithFormat:@"%.02f", tenTip];
tipFieldFifteen.text = [NSString stringWithFormat:@"%.02f", fifteenTip];
tipFieldTwenty.text = [NSString stringWithFormat:@"%.02f", twentyTip];
totalFieldTen.text = [NSString stringWithFormat:@"%.02f", newTotal + tenTip];
totalFieldFifteen.text = [NSString stringWithFormat:@"%.02f", newTotal + fifteenTip];
totalFieldTwenty.text = [NSString stringWithFormat:@"%.02f", newTotal + twentyTip];
}
else if (sender == customPercentSlider)
{
int percentage = (int)(customTipPercent * 100);
customPercentLabel.text = [NSString stringWithFormat:@"%i%%", percentage];
float newSliderValue = ((float) percentage) / 100;
customPercentSlider.value = newSliderValue;
customTipPercent = newSliderValue;
}
float customTip = customTipPercent * newTotal;
tipFieldCustom.text = [NSString stringWithFormat:@"%.02f", customTip];
totalFieldCustom.text = [NSString stringWithFormat:@"%.02f", customTip + newTotal];
}
else
{
toggle = YES;
}
}
@end