Tip calculator in iPhone

Source: Internet
Author: User

1. AppDelegate. h


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHAgY2xhc3M9 "p1">

2. AppDelegate. m


3. ViewController. h


4. ViewController. m


# Import "ViewController. h"

@ Interface ViewController ()

@ End

@ Implementation ViewController

@ Synthesize bilField;

@ Synthesize tipFieldTen;

@ Synthesize tipFieldFifteen;

@ Synthesize tipFieldTwenty;

@ Synthesize tipFieldCustom;

@ Synthesize totalFieldCustom;

@ Synthesize totalFieldTen;

@ Synthesize totalFieldFifteen;

@ Synthesize totalFieldTwenty;

@ Synthesize customPercentLabel;

@ Synthesize customPercentSlider;

-(Void) viewDidLoad

{

[SuperviewDidLoad];

BilField. delegate = self;

// Do any additional setup after loading the view, typically from a nib.

}


-(Void) didReceiveMemoryWarning

{

[SuperdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}


-(Void) awakeFromNib // call this method when all GUI elements are loaded.

{

[BilFieldbecomeFirstResponder]; // displays the key used for bilField input.

}


-(BOOL) textFieldShouldReturn :( UITextField *) textField // used to hide the keyboard

{

[TextField resignFirstResponder];

Return YES;

}

-(IBAction) calculate :( id) sender

{

Static BOOL toggle = YES; // mark whether this method is triggered by a user

If (toggle) // if the user inputs or slides the slider from the keyboard

{

Toggle = NO; // set the flag. The next call to this method will be triggered by the program.

NSString * billFieldText = bilField. text;

Float newTotal = [billFieldText floatValue]; // converts the string type to the floating point type

Float customTipPercent = customPercentSlider. value; // obtain the value of the slider.

If (sender = bilField)

{// Determine whether the event is triggered by bilField

If (billFieldText. length

BillTotal = [NSString stringWithFormat: @ "%. 02f", newTotal/10];

Else

BillTotal = [NSString stringWithFormat: @ "%. 02f", newTotal * 10];

BilField. text = billTotal; // update the text displayed on the interface to the formatted result.

NewTotal = [billTotal floatValue]; // set newTotal to a new value.

// Calculate the tip amount based on the three percentages of 10%, 15%, and 20% respectively.

Float tenTip = maid * 0.10;

Float effecteentip = newTotal * 0.15;

Float twentyTip = maid * 0.20;

// Set the value displayed in the Tip bar

TipFieldTen. text = [NSString stringWithFormat: @ "% 0.2f", tenTip];

TipFieldFifteen. text = [NSString stringWithFormat: @ "% 0.2f", fifteenTip];

TipFieldTwenty. text = [NSString stringWithFormat: @ "% 0.2f", twentyTip];

// Set the value displayed in the Total column

TotalFieldTen. text = [NSString stringWithFormat: @ "% 0.2f", newTotal + tenTip];

TotalFieldFifteen. text = [NSString stringWithFormat: @ "% 0.2f", newTotal + deleteentip];

TotalFieldTwenty. text = [NSString stringWithFormat: @ "% 0.2f", newTotal + twentyTip];


}

Else if (sender = customPercentSlider) // determines whether the event is triggered by customPercentSlider.

{

Int percentage = (int) (customTipPercent * 100); // convert the percentage of the slider to an integer.

CustomPercentLabel. text = [NSString stringWithFormat: @ "% I %", percentage]; // update the tip percentage displayed on the tag, in the form of XX %

Float newSliderValue = (float) percentage)/100; // convert the percentage to a decimal number and assign it to the slider.

CustomPercentSlider. value = newSliderValue;

CustomTipPercent = newSliderValue; // you can call this operation to update the value of customTipPercent based on the ratio of the slider.

}

// Calculate the customTip Value

Float customTip = customTipPercent * newTotal;

TipFieldCustom. text = [NSString stringWithFormat: @ "%. 02f", customTip];

// Update totalFieldCustom

TotalFieldCustom. text = [NSString stringWithFormat: @ "%. 02f", customTip + newTotal];

}

Else

{

Toggle = YES;

}

}

@ End




After running:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.