第二彈~
以前我作的Hello World app,好多人看了都以爲太簡單了。。但畢竟是入門項目,我以爲先放一個功能就夠了。
千里之行,始於足下。一步一個腳印,相信咱們都能成長的很快的。swift
此次,作的是小費計算的app。咱們都知道,在美國,在哪吃個飯都要付小費,可是發票上不必定都有具體的數值,而只是標了三個百分比選項,15%,18%,20%。而每次都要打開手機上的Calculator我的以爲有些麻煩,因此,不如直接開發一個app來計算吧。每次吃完飯內心都有個數吧。app
基本功能很是,很是之簡單。。主要仍是熟悉iOS的組件吧ide
輸入消費總額函數
輸入小費百分比測試
獲得總價spa
建立projectcode
在main storyboard添加組件orm
Labelip
TextField開發
Button
爲組件寫相應函數
TextField1: 消費總額
TextField2: 小費比例
Label: 顯示總價
Button: 點擊計算小費並賦予總價Label.text
import UIKit class ViewController: UIViewController { @IBOutlet weak var txtBill: UITextField! @IBOutlet weak var txtTip: UITextField! @IBOutlet weak var lblTipAmount: UILabel! @IBAction func CalculateTip(_ sender: Any) { let billAmount = (Double)(txtBill.text!) let pctTip = (Double)(txtTip.text!) let tipAmount = billAmount! * pctTip!/100 let tipString = String(format:"%.2f",tipAmount) lblTipAmount.text = "$" + tipString } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
測試
Reference
App school for Xcode and iOS 10 Development Free
Copyright © 2017 zhiwei xu. All rights reserved.