你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iPhone中的小費計算器

iPhone中的小費計算器

編輯:IOS開發綜合

1.AppDelegate.h

\

<喎?/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//當所有GUI的元素被都載入後,調用此方法。

{

[bilFieldbecomeFirstResponder];//顯示用於bilField輸入的鍵

}


-(BOOL)textFieldShouldReturn:(UITextField *)textField//用於隱藏鍵盤

{

[textField resignFirstResponder];

return YES;

}

-(IBAction)calculate:(id)sender

{

static BOOL toggle =YES;//標記此方法是否有用戶觸發

if (toggle)//如果用戶從鍵盤輸入或者滑動滑動條

{

toggle = NO;//設置標志位,此方法的下次調用將由程序觸發

NSString *billFieldText = bilField.text;

float newTotal = [billFieldText floatValue];//字符串類型轉化為浮點類型

float customTipPercent = customPercentSlider.value;//獲得滑動條的值

if (sender==bilField)

{//判斷事件是否有bilField觸發

if (billFieldText.length

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

else

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

bilField.text = billTotal;//將界面上顯示的文本更新為格式化後的結果

newTotal = [billTotal floatValue];//將newTotal設置為新的值

//按照10%,15%,20%三個百分比分別計算小費金額

float tenTip = newTotal*0.10;

float fifteenTip = newTotal*0.15;

float twentyTip = newTotal*0.20;

//設置Tip欄中顯示的值

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

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

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

//設置Total欄中顯示的值

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

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

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


}

else if(sender==customPercentSlider)//判斷事件是否是有customPercentSlider觸發的

{

int percentage = (int)(customTipPercent *100);//將滑動條的百分比轉化成一個整數

customPercentLabel.text = [NSString stringWithFormat:@"%i%%",percentage];//更新標簽上顯示的小費百分比,以XX%的形式顯示

float newSliderValue = ((float)percentage)/100;//將百分比轉化成小數,並賦值給滑動條

customPercentSlider.value = newSliderValue;

customTipPercent = newSliderValue;//根據滑動條的比例更新customTipPercent的值

}

//計算customTip的值

float customTip = customTipPercent * newTotal;

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

//更新totalFieldCustom

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

}

else

{

toggle = YES;

}

}

@end


\


運行之後:


  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved