Change Found a more accurate way!
One argument a button can get is (id)sender . This means that you can create a new button that inherits from UIButton, which allows you to store other alleged arguments. Hopefully these two snippets illustrate what to do.
myOwnbutton.argOne = someValue [myOwnbutton addTarget:self action:@selector(buttonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
and
- (IBAction) buttonTouchUpInside:(id)sender { MyOwnButton *buttonClicked = (MyOwnButton *)sender;
That was my initial suggestion.
There is a way to achieve the same result, but it is not very. Suppose you want to add a parameter to your navigate method. The code below will not allow you to pass this parameter to navigate .
[button addTarget:self action:@selector(navigate) forControlEvents:UIControlEventTouchUpInside];
To get around this, you can move the navigation method to your own class and set the "parameters" as attributes of this class ...
NavigationAid *navAid = [[NavigationAid alloc] init]; navAid.firstParam = someVariableOne navAid.secondParam = someVariableTwo [button addTarget:navAid action:@selector(navigate) forControlEvents:UIControlEventTouchUpInside];
Of course, you can save the navigation method in the source class and call it navAid if it knows where to find it.
NavigationAid *navAid = [[NavigationAid alloc] init]; navAid.whereToCallNavigate = self navAid.firstParam = someVariableOne navAid.secondParam = someVariableTwo [button addTarget:navAid action:@selector(navigate) forControlEvents:UIControlEventTouchUpInside];
As I said, this is ugly, but it worked for me, and I did not find anyone offering any other working solution.
WoodenKitty Feb 08 '11 at 6:08 2011-02-08 06:08
source share