Is it good practice to have IBOutlet
and IBAction
of the same button in a tableviewcell
?
When are some instances when people have the IBOutlets
in the cell and IBAction
in the viewcontroller
?
Thanks!
Firstly, you need to understand the use of IBOutlet and IBAction. They are used for completely different reasons. We use IBOutlet to help the interface builder to recognise the reference variables for the UI elements and we use IBAction to help the interface builder to recognise an events/actions. So, there is no reason for you to not to specify an IBOutlet of a button and IBAction of that button in the same class or in separate classes like IBOutlet in the TableViewCell class and IBAction in the View Controller where your cell is being loaded.
From my experience, I would say, if you have a button in your Table view cell and you want to fire an event based on that button's click, then it is actually good to have the action method in your CustomTableViewCell class. For instance, let's say, you want to change the button image on click. In such a case, in your didSelectRowAtIndexpath method you can change the select state of the button while you could check the select state of the button in your IBAction method in your view class and set the image.
On the other hand, if there is control flow based on your button click, let's say, based on the button click you are going to a different View Controller then you probably want to set the IBAction in your controller class as you need to have access to your Navigation Controller or View Controllers.
So, it depends what is the purpose of your IBAction method and what kind of access you need to perform your action.