Reputation: 159
While creating a new IBAction Method, I have dragged from the Button in the storyboard to my header file as I should. The Popup that appears I have noticed has an arguments dropdown which offers 3 options which are none, sender and sender and event. What is the difference between 'none' and 'sender' and in what situations would each be used?
Upvotes: 4
Views: 886
Reputation: 131426
Stonz2's answer covers it pretty well.
Some examples where you might want the sender:
Say you have a calculator app, and you have digit buttons and operator buttons. Rather than writing a different IBAction method for every button, you might write a -digitTapped
action and an -operatorTapped
action.
You could add tag values to each button, and then in your action method, interrogate the sender to see what it's tag is.
Another example would be a slider. You might use the sender parameter to get a pointer to the slider and fetch it's value.
(BTW, by default IB makes the type of the sender be id, which is an anonymous pointer. I usually change the type to be the type of the object that is triggering the action, like UIButton, UISlider, etc.)
Upvotes: 2
Reputation: 6396
None
You don't need to know any information about what triggered the action, just that the action was triggered.
Sender
You not only need to know that an action was triggered, but information about what object triggered the action. For instance, if you need to know which button triggered a certain action in order to change its properties.
Sender and event
You need to know the action was triggered, what object triggered this action, and the type of event that triggered the action. For example, if you need to know which button triggered a certain action in order to change its properties, and you will change them differently if they touch down on the button vs touch up vs double-tap vs etc. but you don't want to create a separate action method for each type of event.
Upvotes: 6