【问题标题】:How to increase/decrease price label value with button?如何使用按钮增加/减少价格标签值?
【发布时间】:2019-03-09 11:45:53
【问题描述】:

我有两个标签。第一个是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 表视图选定的单元格到 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)

【问题讨论】:

    标签: ios label swift4.2


    【解决方案1】:

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

    【讨论】:

    • 是的,这是正确的,更有意义。 foodPriceLabel 的值不像 foodPrice: 10 那样恒定,它是来自前一个表视图的单元格值的值。所以它是动态变化的。我需要把我的 foodPrice: UILabel 放到 ViewModel(foodPrice: foodPriceLabel) @lukas-würzburger
    • @EmreDeğirmenci 我更新了didSet 函数,使其更加清晰。在 ViewModel 中,您只保存数据。在 didSet 函数中更新 UI。
    • 您的示例很有用,但我没有使用预览屏幕上的动态价格更改foodPrice: 10 参数。 foodPrice: 10 只是用 10 * 数量计算。当我在 MainVC 中点击表格视图的每个单元格时,我的食物名称和价格会转到 DetailVC。当价格来到 DetailVC 时,我需要使用增加/减少按钮更新此价格。 @LukasWürzburger
    • 设置标签文字时始终计算价格:foodPriceLabel.text = Double(viewModel.quantity) * viewModel.foodPrice
    猜你喜欢
    • 2021-08-13
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    相关资源
    最近更新 更多