【问题标题】:How to divide the value of a number in a "Label" field by the value of a UIStepper?如何将“标签”字段中的数字值除以 UIStepper 的值?
【发布时间】:2021-01-28 01:25:49
【问题描述】:

我正在尝试编写一个允许某人拆分账单小费的应用程序。我遇到困难的部分是实现一个 UIStepper,它将获取“splitTotal”字段的值并将其除以步进器的值。代码:

@IBAction func updateTip(_ sender: Any) {
        let tipPercentages = [0.15, 0.20, 0.25]
        
        let bill = Double(billField.text!) ?? 0
        let tip = bill * tipPercentages[tipController.selectedSegmentIndex]
        let total = bill + tip

        let split = total / stepper
        
        tipLabel.text = "$\(tip)"
        totalLabel.text = "$\(total)"
        splitTotal.text = "$\(total)"
        
        tipLabel.text = String(format: "$%.2f",tip)
        totalLabel.text = String(format: "$%.2f",total)
        splitTotal.text = String(format: "$%.2f",split)

【问题讨论】:

    标签: ios swift iphone xcode uistepper


    【解决方案1】:

    当您使用 IBAction 创建操作时,请确保使用正确的发件人来执行此操作。所以你的代码的第一行应该是:

    @IBAction func updateTip(_ sender: UIStepper) {
    

    (将“Any”替换为“UIStepper”)这样,您将继承正确的信息来执行计算。

    接下来,而不是输入:

    let split = total / stepper
    

    你可以这样做:

    let split = total / sender.value
    

    本例中的 sender.value 已经是 Double,因此执行其余的算术应该没有问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-11
      • 2018-05-13
      • 2018-12-29
      • 2019-10-28
      • 1970-01-01
      相关资源
      最近更新 更多