-2

How do I update a UILabel which calculates the total of two amounts and prints the answer on the label?

I have $50 and want to add $50 to the total on the label every time the button is pressed.

One of the amounts would be the total that is already printed on the label from the first click of the button. So I want to click the button again and it to add the 50 each time I click.

I'm using the latest versions of xcode and swift.

The button and label are connected and tested by printing to console.

func totalMoneyUpdate() {
    totalMoney.text = "$50.00"
    // this gives me $50 on the label but don't add it each time i hit the button.

    // I have tried to change the label to a Int but have fail.
    totalMoney:Int = $50.00
    //and tried totalMoney:Int + $50.00

    //I tried
    var a = 25
    var b = 25
    let totalMoney.text = (a + b)  // this only prints $50 to the label once.

    //I tried
    totalMoney.text = "50.00"
    var a = (totalMoney.text as! NSString).floatValue
    var b = (totalMoney.text as! NSString).floatValue
    var sum = a + b
    totalMoney.text = "\(sum)"
    // this prints 100 to the label but don't do anything next time I press.
}     

I think I somehow need to update the label each time button is pressed.

I expect the output to add 50 to the total every time I click the button and display the total on the label.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Kevin
  • 9
  • 2
    Welcome to Stackoverflow. Please format the code / text. – vadian Aug 08 '19 at 13:50
  • 3
    Don’t use controls to store values. Keep the total in a property and update that and use it to create your label. – vacawama Aug 08 '19 at 14:11
  • Please be specific about which versions you're using. "Latest versions" isn't useful beyond a few days, as new versions come out all the time. – Jonathan Hall Aug 09 '19 at 09:47

1 Answers1

1

From what i have understood you want to add 50 every time you click a button. You could try the below code.

//Making local variable to keep track of money added
var addedMoney : Double = 0.00
// amount you want to add every button click
var addFifty : Double = 50.00

override func viewDidLoad() {
    super.viewDidLoad()
    // making sure totalMoney label is shown as 0.00 at start up
    convertToCurrency(value: addedMoney)
}

@IBAction func addMoney(_ sender: Any) {
    // in button click you want to get the local variable and addFifty
    addedMoney = addedMoney + addFifty
    convertToCurrency(value: addedMoney)
}

func convertToCurrency(value : Double) {
    let currencyFormatter = NumberFormatter()
    currencyFormatter.usesGroupingSeparator = true
    currencyFormatter.numberStyle = .currency
    currencyFormatter.locale = Locale.current
    if let priceString = currencyFormatter.string(from: value as NSNumber) {
        totalMoney.text = priceString
    }
}
chirag90
  • 2,211
  • 1
  • 22
  • 37
  • 2
    You never format currencies with string interpolation. – Alexander Aug 08 '19 at 14:11
  • 1
    Don't use String formatting either. You're hardcoding a format that only a minority of people in the world use. "However you might think currencies are formatted, you're probably wrong. Just compare:" https://stackoverflow.com/a/42519166/3141234 – Alexander Aug 08 '19 at 14:23
  • 1
    Ooo earlier I meant "You *should* never...". Typo. – Alexander Aug 08 '19 at 14:29
  • @Alexander Thanks for that i have learned one or two things. Just updated the answer. Hopefully this better? – chirag90 Aug 08 '19 at 14:35
  • Yep, much better. In general, just remember that whatever is the "obvious choice" for you is very likely used by a minority of people in the world. If all American software engineers did this, many apps' temperatures would be in Fahrenheit, even though they're among the only ones to not use Celsius. In general, the output of a String initializer is *almost never* suitable presenting to an end-user. Luckily, Foundation makes "doing the right thing" incredibly easy, with things like `MeasurementFormatter`, `DateFormatter`, `NumberFormatter`, `ListFormatter`, etc. – Alexander Aug 08 '19 at 14:45
  • @Alexander Yes that makes complete sense. Thank you once again. – chirag90 Aug 08 '19 at 14:49