user10318011
user10318011

Reputation:

Use swift stepper to multiply my original number not previous number

Here's my code.

My need is if the price label have had number

When pressing add from stepper

I need stepper value to multiply my price number

but I encounter this situation

The price label won't multiply my original number

like if price number originally is 50

I would like to show 50,100,150,200,250

not like this 50 ,100 ,300,400

@IBOutlet weak var stepperValue: UILabel!
@IBOutlet weak var price: UILabel!

@IBAction func stepper(_ sender: UIStepper) {

    let count = Int(sender.value)
    stepperValue.text = String(count)
    let price = Int(price.text!)!
    price.text? = String(price * count)
}

Is there proper way to solve my logical problem?

Upvotes: 2

Views: 303

Answers (1)

Ievgen Leichenko
Ievgen Leichenko

Reputation: 1317

You need to store original value somewhere and use it for incrementation.

E.g.

@IBOutlet weak var stepperValue: UILabel!
@IBOutlet weak var price: UILabel!

private var originalPrice: Int = 50 // or whatever you want

@IBAction func stepper(_ sender: UIStepper) {

    let count = Int(sender.value)
    stepperValue.text = String(count)

    price.text = String(originalPrice * count)
}

Upvotes: 2

Related Questions