Search code examples
iosswiftcustom-cell

How to make one IBAction that does the same action to two buttons when the first one is from a custom cell and the other one is from storyboard


This is from the custom cell:

img

This is from the Storyboard:

img

Anyone have any idea how to make these two buttons do the exact same action? Is it possible to make multiple buttons do one action?


Solution

  • I usually keep the backing UIViews and UIViewControllers very thin in my applications and I create classes that do the actual heavy lifting. This makes for cleaner code and easier reuse.

    class Player {
         static func play() { // Static makes it easier to call from every class, assuming you only play one song at a time
              // play logic goes here
         }
    }
    
    class MyView: UIView {
         @IBAction func play() { // connect in IB in your .xib
              Player.play()
         }
    }
    
    class MyViewController: UIViewController {
         @IBAction func play() { // connect in IB in your storyboard
              Player.play()
         }
    }