当用户打开开关时,我希望在UILabel上显示一个随机值,然后获取相同的随机值,并使用它乘以并找到另一个值。我认为我的问题是我试图使用let语句来保存该值,然后尝试从另一个函数回调该let值,但是我不知道另一种方法。

激活开关后,我已经可以将随机值显示在UILabel上。然后,当该开关处于非活动状态时,UILabel上将保留值0。

交换机的IBOutlet:

@IBOutlet weak var tipSwitch: UISwitch!


这是切换操作:

@IBAction func switchChanged(_ sender: UISwitch) {
    if sender.isOn{
        sender.setOn(false, animated: true)
        percentPlaceholder.text! = String(0) + "%"


    }
    else{
        sender.setOn(true, animated: true)
        let randomTip = Int.random(in: 1...100)
        percentPlaceholder.text! = String(randomTip)
        calculateTipSwitch()
    }

}


这是calculateTipSwitch函数:

func calculateTipSwitch() {
    var tipAmountSwitch = Float()
    var totalCostSwitch = Float()
    if let billAmountSwitch = Float(billCost.text!) {
        tipAmountSwitch = billAmountSwitch * randomTip / 100
        totalCostSwitch = tipAmountSwitch + billAmountSwitch
    }
    else {
        tipAmountSwitch = 0
        totalCostSwitch = 0
    }
    tipDollarAmt.text! = String(format: "%.2f", tipAmountSwitch)
    totalBill.text! = String(format: "%.2f", totalCostSwitch)
}


如果您想更好地理解我要完成的工作,请参见以下所有代码:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var billCost: UITextField!
    @IBOutlet weak var percentPlaceholder: UILabel!
    @IBOutlet weak var tipDollarAmt: UILabel!
    @IBOutlet weak var totalBill: UILabel!
    @IBOutlet weak var tipSlider: UISlider!
    @IBOutlet weak var tipSegment: UISegmentedControl!
    @IBOutlet weak var tipStepper: UIStepper!
    @IBOutlet weak var tipSwitch: UISwitch!
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let allowed = CharacterSet(charactersIn: ".1234567890")
    return string.rangeOfCharacter(from: allowed) != nil
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    billCost.resignFirstResponder()
    return true
}

override func viewDidLoad() {
    super.viewDidLoad()
    percentPlaceholder.text = ""
    tipDollarAmt.text = ""
    totalBill.text = ""
}
@IBAction func sliderChanged(_ sender: UISlider) {
    percentPlaceholder.text! = String(Int(sender.value)) + "%"
    tipStepper.value = Double(Int(tipSlider.value))
    calculateTip()
}
@IBAction func selectorChanged(_ sender: UISegmentedControl) {

}
@IBAction func stepperChanged(_ sender: UIStepper) {
    percentPlaceholder.text! = String(sender.value) + "%"
    tipSlider.value = Float(tipStepper.value)
    calculateTipStep()
}
@IBAction func switchChanged(_ sender: UISwitch) {
    if sender.isOn{
        sender.setOn(false, animated: true)
        percentPlaceholder.text! = String(0) + "%"


    }
    else{
        sender.setOn(true, animated: true)
        let randomTip = Int.random(in: 1...100)
        percentPlaceholder.text! = String(randomTip)

    }

}

func calculateTip()
{
    var tipAmount = Float()
    var totalCost = Float()
    if let billAmount = Float(billCost.text!) {
        tipAmount = billAmount * tipSlider.value / 100
        totalCost = tipAmount + billAmount
    }
    else {
        tipAmount = 0
        totalCost = 0
    }
    tipDollarAmt.text! = String(format: "%.2f", tipAmount)
    totalBill.text! = String(format: "%.2f", totalCost)
}
func calculateTipStep() {
    var tipAmountStep = Float()
    var totalCostStep = Float()
    if let billAmountStep = Float(billCost.text!) {
        tipAmountStep = billAmountStep * Float(tipStepper.value) / 100
        totalCostStep = tipAmountStep + billAmountStep
    }
    else {
        tipAmountStep = 0
        totalCostStep = 0
    }
    tipDollarAmt.text! = String(format: "%.2f", tipAmountStep)
    totalBill.text! = String(format: "%.2f", totalCostStep)
}
func calculateTipSwitch() {
    var tipAmountSwitch = Float()
    var totalCostSwitch = Float()
    if let billAmountSwitch = Float(billCost.text!) {
        tipAmountSwitch = billAmountSwitch * randomTip / 100
        totalCostSwitch = tipAmountSwitch + billAmountSwitch
    }
    else {
        tipAmountSwitch = 0
        totalCostSwitch = 0
    }
    tipDollarAmt.text! = String(format: "%.2f", tipAmountSwitch)
    totalBill.text! = String(format: "%.2f", totalCostSwitch)
}
}


基本上我的问题是我不能在另一个函数中使用该随机数,所以我只需要有关如何回调该randomTip的帮助。

最佳答案

您可以向calculateTipSwitch添加参数,并将随机数作为参数传递给calculateTipSwitch方法。将calculateTipSwitch更改为:

func calculateTipSwitch(tipPercentage: Int) {
    var tipAmountSwitch = Float()
    var totalCostSwitch = Float()
    if let billAmountSwitch = Float(billCost.text!) {
        tipAmountSwitch = billAmountSwitch * tipPercentage / 100.0
        totalCostSwitch = tipAmountSwitch + billAmountSwitch
    }
    else {
        tipAmountSwitch = 0
        totalCostSwitch = 0
    }
    tipDollarAmt.text! = String(format: "%.2f", tipAmountSwitch)
    totalBill.text! = String(format: "%.2f", totalCostSwitch)
}


然后,在switchChanged中:

let randomTip = Int.random(in: 1...100)
percentPlaceholder.text! = String(randomTip)
calculateTipSwitch(tipPercentage: randomTip)

10-04 12:46