-1

I have these functions:

private func chooseOfferButton1Tapped() {
        let vc = ChooseRdvVC()
        vc.offerCatched = arrayCatched![0]
        HapticsManager.shared.vibrate(for: .success)
        navigationController?.pushViewController(vc, animated: true)
    }
    
    private func chooseOfferButton2Tapped() {
        let vc = ChooseRdvVC()
        vc.offerCatched = arrayCatched![1]
        HapticsManager.shared.vibrate(for: .success)
        navigationController?.pushViewController(vc, animated: true)
    }
                   ...

up to:

                   ...

private func chooseOfferButton50Tapped() {
        let vc = ChooseRdvVC()
        vc.offerCatched = arrayCatched![49]
        HapticsManager.shared.vibrate(for: .success)
        navigationController?.pushViewController(vc, animated: true)
    }

Here's how I call the functions:

let index = "chooseOfferButton\(oneViewSize)Tapped"
chooseOfferButton.addTarget(self,
                            action: Selector(index),
                            for: .touchUpInside)

where oneViewSize is the number that changes

I'm looking for a way to avoid writing this same function over and over only with 2 numbers changing. How can I use a string in a function name? Or a for loop? Or is there another way? Any help is appreciated.

JOHNY
  • 17
  • 5

2 Answers2

1

As mentioned by others there's only one action needed. The index is passed through the tag property of the button.


Instead of putting the index into the selector assign the index (aka oneViewSize) to the tag

chooseOfferButton.addTarget(self,
                            action: #selector(chooseOfferButtonTapped),
                            for: .touchUpInside)
chooseOfferButton.tag = oneViewSize

On the callee side get the index from the button (the sender)

@objc private func chooseOfferButtonTapped(_ sender: UIButton) {
    let vc = ChooseRdvVC()
    let index = sender.tag
    vc.offerCatched = arrayCatched[index]
    HapticsManager.shared.vibrate(for: .success)
    navigationController?.pushViewController(vc, animated: true)
}

Ans please declare arrayCatched non-optional as you force unwrap it anyway

vadian
  • 274,689
  • 30
  • 353
  • 361
0

If you are using table view then you need to use the tag property of UIButton in your cellForRowAt indexPath method

cell.button.tag = indexPath.row
cell.button.addTarget(self, action: #selector(chooseOfferButtonTapped(_:)), for: .touchUpInside)
@objc func chooseOfferButtonTapped(_ sender:UIButton) {
    let vc = ChooseRdvVC()
    vc.offerCatched = arrayCatched[sender.tag]
    HapticsManager.shared.vibrate(for: .success)
    navigationController?.pushViewController(vc, animated: true)
}

Or if you are using @IBOutletCollection then you need to set the tag for each button and set same button click action for each button

 @IBOutlet var btnChooseOffer: [UIButton]!

 @IBAction func chooseOfferButtonTapped(_ sender: UIButton) {
  let vc = ChooseRdvVC()
    vc.offerCatched = arrayCatched[sender.tag]
    HapticsManager.shared.vibrate(for: .success)
    navigationController?.pushViewController(vc, animated: true)

}
Shabnam Siddiqui
  • 579
  • 4
  • 13