UISwitch 保存值以在函数中使用

UISwitch saving values to use in a function

当用户打开开关时,我想要一个随机值显示在 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)

在 switchChanged() 函数中,您将 randomTip 设置为 percentPlaceholder.text,因此在其他函数中,您可以调用它。

let randomTip = percentPlaceholder.text

或者另一种方法是在函数外部声明 randomTip 变量,作为 class.

的 属性
class ViewController : UIViewController {

    //IBOutlets here 

    var randomTip : String?

    //functions here
}

然后在switchChanged()中给它赋值后,就可以在其他函数中调用了。