Search code examples
iosxcodeibaction

In Swift, why does IBAction take the button as a parameter if its linked to only that one button?


I'm giving myself a crash course on functions and what I can't seem to understand is when using storyboard to create an IBAction by clicking and dragging from a UIButton, why does it give you the option to create a parameter for the button if the button already corresponds to the IBAction you just created? Why would creating the button as a parameter be helpful?This isn't clear to me yet but I really want to understand.

Thank you in advance.

enter image description here


Solution

  • You can connect more than one button to the same action. The button parameter then tells you which button was tapped.

    Even when only connected to one button, the parameter is useful. You may wish to access the button in the action. Having the parameter gives you that direct access. No need to access any outlet property you may or may not have.