Search code examples
iosswiftxcodeuiviewcontrollerxib

Link an @IBAction to a button in a .xib view from a ViewController


I have created a .xib file for a view that is going to be repetitive in my iOS app, inside of which there is a UIButton.

I have included that .xib view inside multiple UIViewControllers in my storyboard. I would like to link an @IBAction and an @IBOutlet to the button inside my .xib view that is specific to each UIViewController. In other words, I want every UIViewController to completely manage and handle the UIButton that is inside the .xib view.

Any idea if the above is feasible?


Solution

  • There are a couple of ways to do what you want.

    The way I would do it is to give your custom view a closure that is run when the IBAction method is triggered. And each view controller that loads the view from the xib can pass in the closure to the view and the action will run when the button is clicked.