如何为同一个 UIButton 分配不同的操作?

How to assign different actions for same UIButton?

第一次点击按钮时,按钮的标题将从“添加到购物车”更改为“转到购物车”。从下一次点击按钮将导航到下一个屏幕(购物车页面)[就像 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)
        }
    }

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

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

}

您可以检查按钮标题以执行两种不同的操作。此外,您同时显示警报和推送视图控制器,这可能是屏幕无法导航的原因。

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

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

 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: { [=12=].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
        }