I have custom file called Menu
import Foundation
class Menu:NSObject{
var itemName : String?
var itemURL : String?
var objectId: String?
var created: NSDate?
var updated: NSDate?
}
I am using iCarousel to populate my view and for each cell, I am adding the views manually as with the below code
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
var myView : UIView
let menu = menuItems[index]
myView = UIView(frame: CGRect(x: 0, y: 0, width: 220, height: 500))
myView.backgroundColor = UIColor.white.withAlphaComponent(0.7)
addButton = UIButton(frame: CGRect(x: 150, y: 260, width: 60, height: 20))
addButton.backgroundColor = UIColor.brown
addButton.layer.cornerRadius = 0.5 * addButton.bounds.width
addButton.setTitle("Add", for: .normal)
addButton.titleLabel?.font = UIFont.systemFont(ofSize: 13)
addButton.addTarget(self, action:#selector(addItemToCart(menu:)), for: .touchDown)
myView.addSubview(addButton)
return myView
}
I have made a class addItemToCart which takes in an input of type Menu, which is to be called whenever the addButton is clicked
func addItemToCart(menu : Menu) -> Void {
print(menu.itemName)
}
But I am not able to pass the menu type object to it. How can I pass the object to addItemToCart function whenever the button is pressed?