iosswiftxcodefunctionuistepper

How to make the total price to go up and down by pressing to the +/- button in stepper?


How to create a function in which as the +/- buttons increase, the total cost of the product will increase? I wrote this kind of code but it doesn't count right

 @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")
} 

Solution

  • You are changing the cost per item each time...

    Let's say you start with counter = 1 and your costLbl has a "5" as its text.

    You tap the plus image, and your code does this:

        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
        
    

    you then set costLbl.text = "10"

    so, the next time you tap "plus"...

        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 !!!!
        
    

    and you set costLbl.text = "30"

    so, the next time you tap "plus"...

        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 !!!!
        
    

    and you set costLbl.text = "120"

    and on and on.

    You need a CostPerItem label, and a TotalPrice label.

    Then, each time through you get the same "Cost per Item" from the CostPerItem label, and display the result of priceInt * counter in the TotalPrice label.

    One thing that may have helped you figure this out sooner would be to put your calculations in a single function... then call that function (with an indicator of "incrementing" or "decrementing"), so you aren't confused about the whole multiply or divide issue.

    Take a look at this:

    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)"
    
                }
                
            }
            
        }
        
    }