gpt4 book ai didi

swift - UISwitch 保存要在函数中使用的值

转载 作者:行者123 更新时间:2023-11-30 10:33:36 31 4
gpt4 key购买 nike

当用户打开开关时,我希望在 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)

关于swift - UISwitch 保存要在函数中使用的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58572368/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com