小费计算的账单金额。不影响提示和总数

时间:2018-07-28 17:21:20

标签: ios swift calculator

下面是我的代码和图片。我已经看过先前有关笑脸问题的问题,但找不到帮助。

此外,在我的代码中哪里有错误,当我在帐单字段中输入数字时,该错误应开始计算小费金额并填写小费和总数?

enter image description here

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var billField: UITextField!
    @IBOutlet weak var tipLabel: UILabel!
    @IBOutlet weak var totalLabel: UILabel!
    @IBOutlet weak var tipControl: UISegmentedControl!

    @IBAction func onTap(sender: AnyObject){
        view.endEditing(true)
    }

    @IBAction func calculateTip(sender: AnyObject) {
        let tipPercentages = [0.18, 0.2, 0.25]

        let bill = Double(billField.text!)!
        let tip = bill * tipPercentages[tipControl.selectedSegmentIndex]
        let total = bill + tip

        tipLabel.text = String (format: "$%.2f", tip)
        totalLabel.text = String (format: "$%.2f", tip)
    }
}

0 个答案:

没有答案