ios - 如何使用按钮增加/减少价格标签值?

标签 ios label swift4.2

我有两个标签。第一个是 quantityLabel,第二个是 priceLabel。当我单击增加或减少按钮 priceLabel 时,会随着 quantityLabel 值增加或减少。 (例如,1 个数量为 15,00 美元,2 个数量为 30,00 美元)

我尝试了以下

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)"
    }
}

编辑:添加了带有完整代码的 DetailVC 我的数据来自 MainVC TableView 所选单元格到 DetailVc 类 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)

最佳答案

addQuantity 方法中,您不更新价格标签,在 decreaseQuantity 方法中,您不能使用标签的文本(字符串)并对其进行数学计算.

我建议使用一个包含两个值的结构。使用结构的好处是它是不可变的。因此,每次更新其中的一个属性时,它都会创建一个新的结构实例。这样我们就可以在每次发生变化时使用 didSet 回调来更新标签。

定义一个 ViewModel 结构

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

更新标签

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
    }
}

关于ios - 如何使用按钮增加/减少价格标签值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55077017/

相关文章:

ios - 更新核心数据模型实体和支持的 NSManagedobject 子类

matlab - 所有子图的一个 ylabel matlab

r - 用文本 : ggplot2 标记最小和最大比例填充渐变图例

swift - iOS RxSwift - 使用 RxTest 和 TestScheduler 进行测试不会结束/终止测试用例

cocoa-touch - 返回 iOS 中正在运行的后台应用程序/进程的列表

ios - 如何让 Expo 提示我的应用程序输入 "Always"位置权限?

swift - 使用 RealmSwift 保存一对多关系对象

xcode - 我该如何解决这个问题 : "this class is not key value coding-compliant for the key imageview"?

CollectionView 中的 iOS UIButton 仅在该单元格上触发自定义动画

java - 输入文本时覆盖 Swing 文本标签