简体   繁体   中英

How to increase/decrease price label value with button?

I have two labels. First one is quantityLabel and the second one is priceLabel . When I click to increase or decrease button priceLabel increase or decrease with quantityLabel value. (eg 1 quantity is $15,00 2 quantity is $30,00)

I tried below

var quantity = 1
var updateFoodPrice: Double? {
    didSet {
        foodPrice.text = "\(Double(quantity) * updateFoodPrice!)"
    }
}

@IBAction func addQuantity(_ sender: Any) {
    if quantity < 30 {
        quantity += 1
        foodQuantity.text = String(quantity)

    }

}

@IBAction func decreasedQuantity(_ sender: Any) {
    if quantity > 0 {
        quantity -= 1
        foodQuantity.text = String(quantity)
        foodPrice.text? *= "\(quantity)"
    }
}

Edit: Added DetailVC with full code My datas come from MainVC table view selected cell to DetailVc class DetailViewController: UIViewController {

@IBOutlet weak var foodTitle: UILabel!
@IBOutlet weak var foodSubTitle: UILabel!
@IBOutlet weak var foodPrice: UILabel!
@IBOutlet weak var drinkPicker: UITextField!
@IBOutlet weak var foodQuantity: UILabel!


var drinkPickerView = UIPickerView()
var selectDrinkType: [String] = []
var detailFoodName = ""
var detailFoodPrice = 0.0

var searchFoods: [String]!
var priceFood: [Double]!

let foods = Food(name: ["Hamburger big mac",
                           "Patates",
                           "Whopper",
                           "Steakhouse"], price: [15.0, 20.0, 25.0, 30.0])
let food: Food! = nil

var foodPriceCount = FoodPriceCount(quantity: 1, foodPrice: 15.0) {

    didSet {
        foodQuantity.text = "\(foodPriceCount.quantity)"
        foodPrice.text = "\(Double(foodPriceCount.quantity) * foodPriceCount.foodPrice)TL"

    }
  }

@IBAction func addQuantity(_ sender: Any) {
    if foodPriceCount.quantity < 30 {
        foodPriceCount.quantity += 1
    }
  }

@IBAction func decreasedQuantity(_ sender: Any) {
    if foodPriceCount.quantity > 0 {
        foodPriceCount.quantity -= 1
    }
    } 

viewDidLoad()

   override func viewDidLoad() {
    super.viewDidLoad()

    foodQuantity.text = "1"

    searchFoods = foods.name
    priceFood = foods.price

    foodTitle.text = detailFoodName
    foodPrice.text = String(detailFoodPrice)

In the addQuantity method you don't update the price label and in the decreaseQuantity method you can't use the label's text (string) and do math with it.

I suggest using a struct, that holds both values. The benefit of using a struct is that is is immutable. So every time you update a property inside of it, it creates a new instance of the struct. That way we can use the didSet callback to update the labels every time there's a change.

Define a ViewModel struct

struct ViewModel {
    var quantity: Int
    var foodPrice: Double
}

Update the labels

var viewModel = ViewModel(quantity: 1, foodPrice: 10) {
    didSet {
        foodQuantityLabel.text = "\(viewModel.quantity)"
        foodPriceLabel.text = "\(Double(viewModel.quantity) * viewModel.foodPrice)"
    }
}

@IBAction func addQuantity(_ sender: Any) {
    if viewModel.quantity < 30 {
        viewModel.quantity += 1
    }
}

@IBAction func decreasedQuantity(_ sender: Any) {
    if viewModel.quantity > 0 {
        viewModel.quantity -= 1
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM