0

第一次单击按钮时,按钮的标题将从“添加到购物车”更改为“前往购物车”。并且从下一次单击按钮将导航到下一个屏幕(购物车页面)[就像 Flipkart 一样]。

这是我的一段代码:

    @IBAction func addToCartbtnTapped(_ sender: Any) {
        if let info = detailInfo {
            let cartData = CartStruct(cartItems: info, cartQuantity: 1)
            self.saveCart(data: cartData)
            showAlert()
            (sender as AnyObject).setTitle("Go to Cart", for: .normal)
            
            let cart = self.storyboard?.instantiateViewController(withIdentifier: "CartViewController") as? CartViewController
            self.navigationController?.pushViewController(cart!, animated: true)
        }
    }

我可以更改按钮的标题。但是,每当我第 n 次单击该按钮时,产品就会被添加到购物车中,屏幕无法导航。

如何解决这个问题?

更新..

    override func viewDidLoad() {
        super.viewDidLoad()
        UserDefaults.standard.string(forKey: "btn")
    }



    @IBAction func addToCartbtnTapped(_ sender: Any) {
        if !Clicked {
            if let info = detailInfo {
                let cartData = CartStruct(cartItems: info, cartQuantity: 1)
                self.saveCart(data: cartData)
                showAlert()
                addingTOCart.setTitle("Go to Cart", for: .normal)
                UserDefaults.standard.set("Go to Cart", forKey: "btn")
                print("Clicked")
                Clicked = true
                return
            }
        }
        
        if Clicked {
            print("Perform Action")
            let cart = self.storyboard?.instantiateViewController(withIdentifier: "CartViewController") as? CartViewController
            self.navigationController?.pushViewController(cart!, animated: true)
        }
    }

这就是我试图存储“转到购物车”状态的方式。但不工作..请建议!

4

3 回答 3

0

您可以对按钮标题应用检查以执行两种不同的操作。此外,您同时显示警报并推送 View Controller,这可能是屏幕无法导航的原因。

@IBAction func addToCartbtnTapped(_ sender: UIButton) {
        if let info = detailInfo {
            switch sender.currentTitle! {
            case "Add to Cart":
                let cartData = CartStruct(cartItems: info, cartQuantity: 1)
                self.saveCart(data: cartData)
                showAlert()
                (sender as AnyObject).setTitle("Go to Cart", for: .normal)
            case "Go to Cart":
                let cartData = CartStruct(cartItems: info, cartQuantity: 1)
                self.saveCart(data: cartData)
                showAlert()
                (sender as AnyObject).setTitle("Go to Cart", for: .normal)
            default:
                print("Default Case")
            }
        }
    }
于 2021-09-15T17:12:21.320 回答
0

添加此代码以检查购物车是否已添加,如果根据您的详细控制器添加更改标题:

 override func viewWillAppear(_ animated: Bool) {
            if let info = detailInfo {
                let buttonTItle = (self.checkCartData(cartInfo: info) ? "Go to Cart"  : "Add to Cart")
                addToCartButton.setTitle(buttonTItle, for: .normal)
            }
        }

接下来,在添加到购物车之前检查。如果已经添加,将导航到购物车页面,否则添加新的购物车项目(也更改按钮标题)。

@IBAction func addToCartbtnTapped(_ sender: Any) {
            if let info = detailInfo {
                if checkCartData(cartInfo: info) {
                    let cart = self.storyboard?.instantiateViewController(withIdentifier: "CartViewController") as? CartViewController
                    self.navigationController?.pushViewController(cart!, animated: true)
                } else {
                    let cartData = CartStruct(cartItems: info, cartQuantity: 1)
                    self.saveCart(data: cartData)
                    showAlert()
                    (sender as AnyObject).setTitle("Go to Cart", for: .normal)
                }
            }
        }

在此处查看购物车数据:

        func checkCartData(cartInfo: jsonstruct) -> Bool {
            guard let cart = self.getCartData() else { return false }
            return (cart.contains(where: { $0.cartItems.name == cartInfo.name }) ? true : false )
        }

使用此方法获取所有购物车数据:

        func getCartData() -> [CartStruct]? {
            let defaults = UserDefaults.standard
            var tempCart: [CartStruct]?
            if let cdata = defaults.data(forKey: "cartt") {
                tempCart = try! PropertyListDecoder().decode([CartStruct].self, from: cdata)
            }
            return tempCart
        }
于 2021-09-16T05:32:17.653 回答
-1
import UIKit

class ViewController: UIViewController {

   @IBOutlet weak var btn: UIButton!
   var Clicked:Bool = false
   override func viewDidLoad() {
       super.viewDidLoad()
       // Do any additional setup after loading the view.
   }

   @IBAction func btnClick(_ sender: Any) {
       if !Clicked {
           btn.setTitle("Click", for: .normal)
           print("Clicked")
           Clicked = true
           return
       }
       
       if Clicked {
           print("Perform Action")
       }
       
   }

}

于 2021-09-15T13:26:35.120 回答