swift 如何通过按步进器中的+/-按钮使总价上升和下降?[已关闭]

lyr7nygr  于 2023-01-01  发布在  Swift
关注(0)|答案(1)|浏览(109)

这个问题是由打字错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
13小时前关门了。
Improve this question
如何创建一个函数,随着+/-按钮的增加,产品的总成本会增加?我写了这样的代码,但不算数

@IBOutlet weak var counterlbl: UILabel! // Counter label
 @IBOutlet var costLbl: UILabel! // Price label
 var counter = 1 //Counter which contains the value to increment or decrement(+/-)

let minusTap = UITapGestureRecognizer(target: self, action: #selector(minusImageTapped))
        minusBtn.addGestureRecognizer(minusTap)
        minusBtn.isUserInteractionEnabled = true
        
//custom UIstepper I used images
        let plusTap = UITapGestureRecognizer(target: self, action: #selector(plusImageTapped))
        plusBtn.addGestureRecognizer(plusTap)
        plusBtn.isUserInteractionEnabled = true
//decrement
  @objc func minusImageTapped () {
    let price = costLbl.text
    var priceInt = Int(price!)
        if counter > 1{
           counter -= 1
            let totalPrice = priceInt! / counter
            counterlbl.text = String(counter)
            costLbl.text = String(totalPrice)
            print("minus")
            
        }
    }

//increment
   @objc func plusImageTapped() {
    let price = costLbl.text
    var priceInt = Int(price!)
    counter += 1
    let totalPrice = priceInt! * counter
    counterlbl.text = String(counter)
    costLbl.text = String(totalPrice)
    print("Plus")
}
fzsnzjdm

fzsnzjdm1#

您每次都在更改*每项成本***...
假设您从counter = 1开始,并且costLbl的文本为“5”。
点击
plus**图像,代码将执行以下操作:

price = costLbl.text                // costLbl text is "5"
    priceInt = Int(price)               // priceInt now equals 5
    counter += 1                        // counter now equals 2
    totalPrice = priceInt * counter     // totalPrice now equals 10

然后设置costLbl.text = "10"
所以,下次你点“加”的时候...

price = costLbl.text                // costLbl text is "10" !!!!
    priceInt = Int(price)               // priceInt now equals 10 !!!!
    counter += 1                        // counter now equals 3
    totalPrice = priceInt * counter     // totalPrice now equals 30 !!!!

并设置costLbl.text = "30"
所以,下次你点“加”的时候...

price = costLbl.text                // costLbl text is "30" !!!!
    priceInt = Int(price)               // priceInt now equals 30 !!!!
    counter += 1                        // counter now equals 4
    totalPrice = priceInt * counter     // totalPrice now equals 120 !!!!

并设置costLbl.text = "120"
等等。
您需要一个*每项成本*标签和一个*总价*标签。
然后,每次都从
CostPerItem
标签中获得相同的“Cost per Item”,并在
TotalPrice
标签中显示priceInt * counter的结果。
有一件事可能会帮助您更快地弄清楚这一点,那就是将您的计算放在一个函数中...然后调用该函数(使用“递增”或“递减”指示符),这样您就不会对整个乘法或除法问题感到困惑。
看看这个:

class PlusMinusVC: UIViewController {
    
    @IBOutlet var costPerItemLbl: UILabel!      // "Cost per Item" label
    @IBOutlet var counterLbl: UILabel!          // "Number of Items" label
    @IBOutlet var totalPriceLbl: UILabel!       // Total Price display label
    
    var counter = 1     // Counter (Number of Items) which contains the value to increment or decrement(+/-)
    
    //decrement
    @objc func minusImageTapped () {
        updateLabels(increment: false)
    }
    
    //increment
    @objc func plusImageTapped() {
        updateLabels(increment: true)
    }

    func updateLabels(increment: Bool) {

        // don't do anything until we make sure we have valid price

        // make sure Cost Per Item label has a string
        if let priceStr = costPerItemLbl.text {
            
            // make sure we get a valid Int
            if let priceInt = Int(priceStr) {

                // if we tapped the minus image,
                //  don't go lower than 1 item
                if increment == false {
                    if counter > 1 {
                        counter -= 1
                    } else {
                        return
                    }
                } else {
                    counter += 1
                }
                
                counterLbl.text = "\(counter)"
                
                let totalPrice = priceInt * counter
                totalPriceLbl.text = "Total: \(totalPrice)"

            }
            
        }
        
    }
    
}

相关问题