按下@IBAction按钮后,Swift保持变量

时间:2015-05-21 20:13:36

标签: swift

我正在研究" tip计算器"我遇到了一个问题。一旦我将信息放入UITextField并运行应用程序,它就可以正确计算。然后我去调整提示滑块值或人数为另一个值,再次点击计算,我的tabBill变量与所有其他值一起被删除。如果我注释掉货币格式化程序,它将按照我的意愿行事。如何让变量保持其值以便我可以对一个字段进行调整,然后在保持货币格式的同时再次运行它?请特别具体,因为我对这个术语很新。

@IBAction func calculateButtonPress(sender: AnyObject) {
        println("refresh requested")

        let numberFormatter = NSNumberFormatter()
        numberFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle

        let tabBill = (billAmountTextField.text as NSString).doubleValue
        billAmountTextField.text = numberFormatter.stringFromNumber(tabBill)
        billAmountTextField.text != "\(tabBill)"

        peoplePaying = (numberOfPeoplePaying.text as NSString).doubleValue

        tipTotal = tabBill * tipPercent

        tipPerPerson = Double(tipTotal) / Double(peoplePaying)
        tipPerPersonAmount.text = "\(tipPerPerson)"
        tipPerPersonAmount.text = numberFormatter.stringFromNumber(tipPerPerson)

        totalPerPersonDue = (tabBill + tipTotal) / peoplePaying
        totalPerPerson.text = "\(totalPerPersonDue)"
        totalPerPerson.text = numberFormatter.stringFromNumber(totalPerPersonDue)

        tipTotalAmount.text = numberFormatter.stringFromNumber(tipTotal)
        totalBillWithTip = Double(tabBill)+Double(tipTotal)

        totalBillAmountWithTip.text = numberFormatter.stringFromNumber(totalBillWithTip)

        //console output
        println("tabBill:\(tabBill)", "sliderValue:\(sliderValue)", "tipPercent:\(tipPercent)", "tipTotal:\(tipTotal)", "tipPerPerson:\(tipPerPerson)", "peoplePaying:\(peoplePaying)", "totalPerPersonDue:\(totalPerPersonDue)", "totalBillWithTip:\(totalBillWithTip)")
}

1 个答案:

答案 0 :(得分:0)

第一次运行应用程序时,文本字段中有一个双精度数(比如10.00),因此转换工作正常,但是您将此双精度转换为货币并保存到文本字段中(现在它的值为10.00美元) ,这个新文本字段无法转换为您第一次使用的方式,尝试通过此函数运行文本字段的值:

func currencyStringAsDouble(currencyString: String) -> Double {
    let currencyFormatter = NSNumberFormatter()
    currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
    let cleanedString = currencyString.stringByReplacingOccurrencesOfString(currencyFormatter.currencySymbol, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.groupingSeparator, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.decimalSeparator, withString: "")
    let currencyDouble = (text as NSString).doubleValue
    return currencyDouble;
}

此功能会将您的货币字符串再次转换回doulbe,您可以这样做:

let tabBill = currencyStringAsDouble(billAmountTextField.text)

所以你的最终代码将如下所示:

@IBAction func calculateButtonPress(sender:AnyObject){         println(“请求刷新”)

    let numberFormatter = NSNumberFormatter()
    numberFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle

    //***Only line that change***//
    let tabBill = currencyStringAsDouble(billAmountTextField.text)

    billAmountTextField.text = numberFormatter.stringFromNumber(tabBill)
    billAmountTextField.text != "\(tabBill)"

    peoplePaying = (numberOfPeoplePaying.text as NSString).doubleValue

    tipTotal = tabBill * tipPercent

    tipPerPerson = Double(tipTotal) / Double(peoplePaying)
    tipPerPersonAmount.text = "\(tipPerPerson)"
    tipPerPersonAmount.text = numberFormatter.stringFromNumber(tipPerPerson)

    totalPerPersonDue = (tabBill + tipTotal) / peoplePaying
    totalPerPerson.text = "\(totalPerPersonDue)"
    totalPerPerson.text = numberFormatter.stringFromNumber(totalPerPersonDue)

    tipTotalAmount.text = numberFormatter.stringFromNumber(tipTotal)
    totalBillWithTip = Double(tabBill)+Double(tipTotal)

    totalBillAmountWithTip.text = numberFormatter.stringFromNumber(totalBillWithTip)

    //console output
    println("tabBill:\(tabBill)", "sliderValue:\(sliderValue)", "tipPercent:\(tipPercent)", "tipTotal:\(tipTotal)", "tipPerPerson:\(tipPerPerson)", "peoplePaying:\(peoplePaying)", "totalPerPersonDue:\(totalPerPersonDue)", "totalBillWithTip:\(totalBillWithTip)")
}

//Add new function to convert currency to double
 func currencyStringAsDouble(currencyString: String) -> Double {
        let cleanedString = currencyString.stringByReplacingOccurrencesOfString(currencyFormatter.currencySymbol, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.groupingSeparator, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.decimalSeparator, withString: "")
        let currencyDouble = (text as NSString).doubleValue
        return currencyDouble;
}