I have 10 programmatically buttons create, they all work except for the first.
The button action works fine for all of the other buttons, but just incase it just checks for the sender.tag and does a different action depending on the sender.
I cannot figure out the root of the problem and I need all of the buttons to work
Code Block for image in 1...10 { let imageName = String("image\(String(image)).png") let imageToDisplay = UIImage(named: imageName) let catagoryButton = UIButton(type: UIButton.ButtonType.custom) as UIButton catagoryButton.setBackgroundImage(imageToDisplay, for: .normal) let xCoor = 20 + 60 * CGFloat(image - 1) contentWidth += 60 catagoryButton.frame = CGRect(x: xCoor, y: 10, width: 40, height: 40) catagoryButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside) catagoryButton.tag = image catagoryScrollView.addSubview(catagoryButton) print("This Buttons tag is \(catagoryButton.tag)") } catagoryScrollView.contentSize = CGSize(width: contentWidth + 20, height: 60)
The button action works fine for all of the other buttons, but just incase it just checks for the sender.tag and does a different action depending on the sender.
Code Block @objc func buttonAction(sender: UIButton!) { print("button number \(sender.tag)") var showThisArray = [CreditCard]() var showThisArrayOfValues = [Double]() if sender.tag == 1 { showThisArray = restaurantCards showThisArrayOfValues = restaurantCardsValues cardReward = "Restaurants" } else if sender.tag == 2 { showThisArray = gasCards showThisArrayOfValues = gasCardsValues cardReward = "Gas Stations" } else if sender.tag == 3 { showThisArray = hotelCards showThisArrayOfValues = hotelCardsValues cardReward = "Hotels" } else if sender.tag == 4 { showThisArray = groceryCards showThisArrayOfValues = groceryCardsValues cardReward = "Grocery Stores" } else if sender.tag == 5 { showThisArray = transitCards showThisArrayOfValues = transitCardsValues cardReward = "Transit Station" } else if sender.tag == 6 { showThisArray = wholesaleCards showThisArrayOfValues = wholesaleCardsValues cardReward = "Wholesalers" } else if sender.tag == 7 { showThisArray = drugStoreCards showThisArrayOfValues = drugStoreCardsValues cardReward = "Drug Stores" } else if sender.tag == 8 { showThisArray = entertainmentCards showThisArrayOfValues = entertainmentCardsValues cardReward = "Entertainment Locations" } else if sender.tag == 9 { showThisArray = supplyCards showThisArrayOfValues = supplyCardsValues cardReward = "Supply Stores" } else if sender.tag == 10 { showThisArray = departmentCards showThisArrayOfValues = departmentCardsValues cardReward = "Department Stores" } else if sender.tag == 11 { showThisArray = otherCards showThisArrayOfValues = otherCardValues cardReward = "Other Categories" } // More code that is not important }
I cannot figure out the root of the problem and I need all of the buttons to work