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