iOS 小项目--小费计算器
這個(gè)例子是 iPhone應(yīng)用程序開(kāi)發(fā)名師解析 上的一個(gè)小例子,基于Xcode4.3.1的工程開(kāi)發(fā)
效果截圖:
??????
1.創(chuàng)建一個(gè)新工程叫TipCalculator; File->New->Project ->single View Application -> next.
2.布局界面
其中包含9個(gè)靜態(tài)Label標(biāo)簽一個(gè)動(dòng)態(tài)Label標(biāo)簽,他根據(jù)slider值的變化而能變化,然后是9個(gè)Textfield和一個(gè)slider滑動(dòng)條
另外需要對(duì)TextField屬性設(shè)置,text設(shè)置為0.00,然后鍵盤選擇成Number Pad類型的,只需要對(duì)第一個(gè)最長(zhǎng)的TextfIeld設(shè)置一下鍵盤類型就行,其他的不用設(shè)置,其他的8個(gè)標(biāo)簽顯示了灰色數(shù)字,設(shè)置的是Placeholder屬性;
??
3.設(shè)計(jì)代碼,我們選擇左上角的Editor圖標(biāo),點(diǎn)擊出現(xiàn)視圖編輯框分為XIB文件,和TipCalculatorViewController.h文件
??
如果沒(méi)有出現(xiàn)TipCalculatorViewController.h,我們可以根據(jù)Automatic文件進(jìn)行切換
??
4.右鍵選中最長(zhǎng)的那個(gè)TextField文件拖動(dòng)到右邊TipCalculatorViewController.h文件@end前面,
然后彈出一個(gè)框框,Connection選為Outlet,Storage選為Strong,
??
5.這樣我們第一個(gè)TextField關(guān)聯(lián)成功,其他的一樣,
關(guān)聯(lián)Slider時(shí)候,我們對(duì)他設(shè)置一些屬性,(原本是最小值設(shè)為0最大值設(shè)為0.3但是我在拖動(dòng)滑動(dòng)條時(shí)候滑動(dòng)條總是自動(dòng)變?yōu)?,這樣修改后問(wèn)題就可以解決了),他還有一個(gè)和calculate的關(guān)聯(lián)方法,
??
我們還需要添加兩個(gè)方法,并聲明一個(gè)NSString類的對(duì)象
總的代碼為
#import <UIKit/UIKit.h> @interface TipCalculatorViewController : UIViewController { NSString *billTotal; } @property (strong, nonatomic) IBOutlet UITextField *billField; @property (strong, nonatomic) IBOutlet UITextField *tipFieldTen; @property (strong, nonatomic) IBOutlet UITextField *tipFieldFifteen; @property (strong, nonatomic) IBOutlet UITextField *tipFieldTwenty; @property (strong, nonatomic) IBOutlet UITextField *totalFieldTen; @property (strong, nonatomic) IBOutlet UITextField *totalFieldFifteen; @property (strong, nonatomic) IBOutlet UITextField *totalFieldTwenty; @property (strong, nonatomic) IBOutlet UITextField *tipFieldCustom; @property (strong, nonatomic) IBOutlet UITextField *totalFieldCustom; @property (strong, nonatomic) IBOutlet UISlider *customPercentSlider; @property (strong, nonatomic) IBOutlet UILabel *customLabel; -(IBAction)calculate:(id)sender;//計(jì)算小費(fèi) -(IBAction)backGrounfTap:(id)sender;//處理隱藏鍵盤 @end然后會(huì)彈出兩個(gè)方法,backGrounfTap和calculate(注.原本是想寫成backGroundTap,單詞寫錯(cuò)了變成backGrounfTap,為了其他地方不再修改,就按照backGrounfTap寫了),xuanzhbackGrounfTap方法,
//處理鍵盤 -(IBAction)backGrounfTap:(id)sender { [billField resignFirstResponder]; [tipFieldCustom resignFirstResponder]; [totalFieldCustom resignFirstResponder]; } 為什么只把三個(gè)TextField注冊(cè)為了第一響應(yīng),完全按照自己意愿添加,但是最低我們要保證billField第一個(gè)添加上,其他的想添加不想添加都無(wú)所謂,只是個(gè)人覺(jué)得多添加的兩個(gè)體驗(yàn)會(huì)好一點(diǎn);7.主要就是calculate方法實(shí)現(xiàn)部分
//當(dāng)調(diào)出的鍵盤被觸摸了某個(gè)數(shù)時(shí)或者某個(gè)按鈕調(diào)用此方法 -(IBAction)calculate:(id)sender { // 標(biāo)記是否是由用戶觸發(fā) static BOOL toggle =YES; if (toggle) { // 設(shè)置標(biāo)志位,此方法的下次調(diào)用由程序觸發(fā) toggle=NO; // 取出billField文本值 NSString *billFieldText = billField.text; // 將billFieldText字符串類型轉(zhuǎn)換成浮點(diǎn)型 float newTotal = [billFieldText floatValue]; // 獲取滑動(dòng)條的值 float customTipPercent = customPercentSlider.value; // 事件由billField觸發(fā) if (sender == billField) { // 通過(guò)比較billField字符長(zhǎng)度,檢測(cè)billField值是否改變 if(billFieldText.length < billTotal.length) billTotal = [NSString stringWithFormat:@"%.02f",newTotal/10]; // 用戶輸入數(shù)字 else billTotal = [NSString stringWithFormat:@"%.02f",newTotal*10]; // 取出billTotal賦給billField文本值 billField.text = billTotal; // 將billTotal轉(zhuǎn)換成浮點(diǎn)型,newTotal設(shè)置為新值 newTotal = [billTotal floatValue]; // 按照10% 15% 20%計(jì)算小費(fèi)值 float tenTip = newTotal*0.1; float fifteenTip = newTotal*0.15; float twentyTip = newTotal*0.2; // 顯示Tip欄小費(fèi)值 tipFieldTen.text = [NSString stringWithFormat:@"%.02f",tenTip]; tipFieldFifteen.text = [NSString stringWithFormat:@"%.02f",fifteenTip]; tipFieldTwenty.text = [NSString stringWithFormat:@"%.02f",twentyTip]; // 設(shè)置toatal欄中顯示值 totalFieldTen.text = [NSString stringWithFormat:@"%.02f",newTotal+tenTip]; totalFieldFifteen.text = [NSString stringWithFormat:@"%.02f",newTotal+fifteenTip]; totalFieldTwenty.text = [NSString stringWithFormat:@"%.02f",newTotal+twentyTip]; } // 當(dāng)事件由customPercentSlider觸發(fā)時(shí) else if (sender == customPercentSlider) { // 顯示滑動(dòng)條變化值 customLabel.text = [NSString stringWithFormat:@"%i%%",(int)customTipPercent]; // 把按鈕值賦給滑動(dòng)條的值,其中對(duì)浮點(diǎn)型值進(jìn)行了強(qiáng)制轉(zhuǎn)換 customPercentSlider.value=(int)customTipPercent; customTipPercent = (int)customTipPercent; } // 計(jì)算小費(fèi),因?yàn)榛瑒?dòng)條值為0~30,所以需要除以100求出小費(fèi)率 float customTip = customTipPercent/100 * newTotal; tipFieldCustom.text = [NSString stringWithFormat:@"%.02f",customTip]; // 表示應(yīng)付的總金額 totalFieldCustom.text =[NSString stringWithFormat:@"%.02f",customTip+newTotal]; } else { toggle=YES; } }8.當(dāng)我們輸入時(shí)候,有個(gè)特別要求,假如用戶小費(fèi)12元,我們應(yīng)輸入1200,然后他們會(huì)自動(dòng)變成12.00元,主要通過(guò)
通過(guò)比較
if
billTotal
用戶輸入數(shù)字
else
billTotal
實(shí)現(xiàn)方法,通過(guò)比較billField原來(lái)字符串長(zhǎng)度檢測(cè)用戶是否改變金額,來(lái)變得小費(fèi)和總的金額;源代碼:http://download.csdn.net/detail/duxinfeng2010/4464095
? ? ?本文轉(zhuǎn)自新風(fēng)作浪 51CTO博客,原文鏈接:http://blog.51cto.com/duxinfeng/1208729,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的iOS 小项目--小费计算器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 向量点积(Dot Product)
- 下一篇: 基于统计学的商务与经济数据分析知识