This question already has an answer here:
I need to pass two actions for single UIButton
.
First argument passed successfully asa follows:
[imageButton addTarget:self action:@selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
imageButton.tag = 1;
But I need to pass another argument also for the same button:
int secondAction =10;
[imageButton addTarget:self action:@selector(imageClicked:*secondAction) forControlEvents:UIControlEventTouchUpInside];
Can anybody help how to pass two values for a single button/selector?
Every event has a sender that can be get at selector method by
now this sender is actually an instance whose attributes could be used to get the information about it now what you can do is you can create a class and add some attribute to it that you want to pass through selector and then use NSNotificationCenter for broad casting your event
put this in the class where you want to recieve the event and have the selector
and
this where you want to throw an event
Short answer: You don't. The
@selector
there tells the button what method to call when it gets tapped, not the arguments that it should pass to the method.Longer answer: If you know when you're creating the button what the argument is going to be, then you can wrap it up like this:
If you don't know, then you probably want to save the argument to a variable somewhere.
You can use Objective C Runtime feature for associating data with objects as :
Step 1 : Import this in your class :
#import <objc/runtime.h>
step 2 : Crete a key name as :
static char * kDataAssociationKey = "associated_data_key";
Step 3 : Associate data with your object (Ex: button) as :
Step 4 : Get associated data in your method as :
Hope it helps you.
One argument that the button can receive is (id)sender. This means you can create a new button, inheriting from UIButton, that allows you to store the other intended arguments. Hopefully these two snippets illustrate what to do.
and
please refer this link for further explanation passing-parameters-on-button-actionselector