4

For my project, I have created an ordering menu that programmatically creates buttons that scroll horizontally. However I am looking for ways to assign IBAction - specially action to write to Firebase database - to those buttons dynamically... Would tagging the buttons and using conditionals work?

My codes are

Food.swift - properties sets of each menu item

class Food
{
    var title = ""
    var featuredImage: UIImage
    var color: UIColor

    init(title: String, featuredImage: UIImage, color: UIColor)
    {
        self.title = title
        self.featuredImage = featuredImage
        self.color = color
    }

    static func fetchFoods() -> [Food]
    {
        return [
            Food(title: "                   Biscuits", featuredImage: UIImage(named: "f1")!, color: UIColor(red: 63/255.0, green: 71/255.0, blue: 80/255.0, alpha: 0.6)),
            Food(title: "                   Sandwich", featuredImage: UIImage(named: "f2")!, color: UIColor(red: 63/255.0, green: 71/255.0, blue: 80/255.0, alpha: 0.6)),
            Food(title: "             Veg Sandwich", featuredImage: UIImage(named: "f3")!, color: UIColor(red: 63/255.0, green: 71/255.0, blue: 80/255.0, alpha: 0.6)),

        ]
    }
}

UICollectionCell swift

import UIKit
class FoodCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var featuredImageView: UIImageView!
    @IBOutlet weak var backgroundColorView: UIView!
    @IBOutlet weak var foodButton: UIButton!
    var food: Food? {
        didSet {
            self.updateUI()
        }
    }
    private func updateUI()
    {
        if let food = food {
            featuredImageView.image = food.featuredImage
            backgroundColorView.backgroundColor = food.color
            foodButton.setTitle("\(food.title)", for: .normal)
        } else {
            featuredImageView.image = nil
            backgroundColorView.backgroundColor = nil
            foodButton.setTitle("", for: .normal)
        }
    }
}

Can I add to Food.swift,

 var buttonTag = "" 

then assign tag to individual button. At UICollectionCell.swift, append tag. Using If...else... conditionals to match tag to individual IBAction {ref?.child...")

Thanks everyone!!!

ps.There is another swift file for arranging the menu items horizontally..

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • You should look into using `addTarget(_ target: Any?, action: Selector, for controlEvents: UIControl.Event)` method on the button. – ovejka Oct 22 '18 at 11:27
  • As the name suggests, `Interface Builder Action` is for `IB` only. Simply add a target. – Desdenova Oct 22 '18 at 11:48

2 Answers2

3

Tags are one way to do it, but the easiest way is probably to call addTarget(_:action:for:) on each one to set up its action, e.g.

button.addTarget(button,
                 action: #selector(buttonFunctionThatDoesSomething:),
                 for: .touchUpInside) // or .primaryActionTriggered

The first argument is the object whose selector (the second argument) is to be called. The #selector() syntax is really picky; you may want to read this SO answer to figure out the right way to call it.

NRitH
  • 13,441
  • 4
  • 41
  • 44
  • Thanks! Without sounding like I am repeating the question: how do you assign addTarget function to a particular button that is programmatically created? I suppose you need to make each button object unique? Will the title of the button - which is programmatically created - do the job? –  Oct 23 '18 at 00:34
0

from the apple document https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ImplementingACustomControl.html. :

 button.addTarget(self, action: #selector(funcName), for: .touchUpInside)

and then:

 @objc func funcName()  {
       //do what u want in here
    }
Mahgolsadat Fathi
  • 3,107
  • 4
  • 16
  • 34
  • 1
    Thanks! Without sounding like I am repeating the question: how do you assign addTarget function to a particular button that is programmatically created? I suppose you need to make each button object unique? Will the title of the button - which is programmatically created - do the job? –  Oct 23 '18 at 01:43
  • I'm not sure if I understood you right, but you can pass your button title to your function as argument and do what you want to – Mahgolsadat Fathi Oct 27 '18 at 11:05