#16餐費換算app

☀成品:

☀︎主要練習項目:
1.字串轉數字
2.設定數字鍵盤(可收起)

☀︎設計想法:
大部分看到的都是將餐費作為均分的設計,不過自己和朋友出門吃飯時,大多是將自己的費用加上服務費轉給付錢的人,因此想要設計可以算出每個人的價格,然後首頁會自動加成這單的總金額給付錢的人核對總金額是否正確。
⚠️ 想法很美好不過技術還不允許,所以這是一個半成品。
Problem1:
• 產生的分頁無法寫程式 →新增類別並繼承Cocoa Touch. framework
參考:https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E6%96%B0%E5%A2%9E-cocoa-touch-class-%E7%9A%84%E6%AD%A5%E9%A9%9F%E8%AA%AA%E6%98%8E-5601a0b2ed3b

Problem2:(找了資料試了一下還是不太懂如何使用,學習完再來補上)
• 每位個人的金額回傳至總金額相加 →protocol&delegate應用
參考:
https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E5%88%A9%E7%94%A8-delegate-%E8%AE%93%E4%B8%8D%E5%90%8C%E5%85%83%E4%BB%B6%E6%BA%9D%E9%80%9A-%E4%BB%A5%E8%B3%87%E6%96%99%E5%9B%9E%E5%82%B3%E5%88%B0%E5%89%8D%E4%B8%80%E9%A0%81-controller-%E7%82%BA%E4%BE%8B-ba215f3d2596

☀︎製作流程:

  1. textField屬性設定:

⭐︎ clear button為輸入時旁邊可以消除的叉叉

⭐︎預設鍵盤為輸入number的

2.func設定:(主要分為計算和清除)

⭐︎計算:
• 首先resignFirstResponder()為輸入完畢時,能夠關閉鍵盤
• if為未輸入空字串時則顯示為0,else的部分則先計算小費後,再將小費跟原
價格相加,作為個人的價格。
(參考:https://ithelp.ithome.com.tw/articles/10220888https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/22-%E6%8F%9B%E7%AE%97app-%E5%A4%9A%E4%BA%BA%E5%88%86%E6%94%A4%E8%A8%88%E7%AE%97%E6%A9%9F-c8c589030643

 @IBAction func calculateButton(_ sender: Any) {
mealPrice.resignFirstResponder()
serviceCharge.resignFirstResponder()
if mealPrice.text == "" || serviceCharge.text == ""{
mealPrice.text = "0"
serviceCharge.text = "0"
}else{
let tip = Double(mealPrice.text!)! * Double(serviceCharge.text!)! / 100
let price = Double(mealPrice.text!)! + tip

individualPrice.text = String(price) + "元"

}
}

⭐︎清除:

  • 按下清除後,顯示空字串及0元
    @IBAction func clearButton(_ sender: Any) {

mealPrice.text = ""
serviceCharge.text = ""
individualPrice.text = "0元"
}

最後附上GitHub網址:

import UIKit

class peopleOneViewController: UIViewController {


@IBOutlet weak var mealPrice: UITextField!
@IBOutlet weak var serviceCharge: UITextField!
@IBOutlet weak var individualPrice: UILabel!


override func viewDidLoad() {
super.viewDidLoad()

mealPrice.keyboardType = .numberPad
serviceCharge.keyboardType = .numberPad

// Do any additional setup after loading the view.
}

@IBAction func calculateButton(_ sender: Any) {
mealPrice.resignFirstResponder()
serviceCharge.resignFirstResponder()
if mealPrice.text == "" || serviceCharge.text == ""{
mealPrice.text = "0"
serviceCharge.text = "0"
}else{
let tip = Double(mealPrice.text!)! * Double(serviceCharge.text!)! / 100
let price = Double(mealPrice.text!)! + tip

individualPrice.text = String(price) + "元"

}
}

@IBAction func clearButton(_ sender: Any) {

mealPrice.text = ""
serviceCharge.text = ""
individualPrice.text = "0元"
}

https://github.com/ji3g4vic/mealCalculate-undone-

--

--