views:

407

answers:

6

In the following, how can I send two arguments in togButton method? I tried, but it did not work.

[button addTarget:self action:@selector(togButton:) 
             forControlEvents:UIControlEventTouchUpInside];
+3  A: 

control targets only accept one argument for their actions: the control that's being manipulated. There's no way to do what you want (the OS just doesn't support it).

Ben Gottlieb
Actually, it's entirely valid to accept zero or two arguments as well. See my answer. In any case, the parameters are predefined and another method (such as subclassing) must be used to pass app-specific data to the action selector.
gerry3
Actually, it is supported via NSInvocation
Zoran Simic
+2  A: 

What are you trying to put in there? Maybe you can use the tag property, or if that's not sufficient, subclass UIButton and add instance variables that you can then access in -togButton:.

Thomas Müller
+1  A: 

The best way to send two arguments to the method is to wrap them in a class. For example, if the two are arguments are strings then you would have a class solely comprised of string iVars.

zPesk
A: 

[Class instancesRespondToSelector: @selector (setTo:over:)]

I am not an Objective-C guru, but I remember seeing this code in a book.

Sumit M Asok
if this is a misunderstanding of mine, please let me know.
Sumit M Asok
That tests wether or not instances can handle the setTo:over: message. It doesn't help with sending multiple arguments in a target action. What other languages do you know? I'll try to translate the statement to an equivalent.
outis
@outisbut this code is shown in Stephen's book like this,if ( [mySquare respondsToSelector: @selector (setWidth:andHeight:)] == YES ) NSLog (@”mySquare responds to setWidth:andHeight: method”);Result,mySquare responds to setWidth:andHeight: method...?I was thinking it works, for SEL also.I am not arguing, just making myself clear...about idea.
Sumit M Asok
@outis, i know c and a bit of java too.
Sumit M Asok
The code in your answer is basically equivalent to `try {Class<?>[] types={int.class, int.class}; MyClass.class.getMethod("setToOver", types); hasMethod=true; } catch (NoSuchMethodException e) {hasMethod=false;}`, assuming the `setToOver` method takes two ints. It merely tells you whether a class has a method; it doesn't call the method. See http://java.sun.com/docs/books/tutorial/reflect/ for more info on `getMethod` and reflection.
outis
A: 

The action selector called by a button tap can have zero, one, or two parameters. The first is the sender (the button that was tapped) and the second is the event (the tap). See the target-action mechanism for controls:

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event

As Thomas Müller suggests, the best way to pass app-specific data to your action selector is by subclassing UIButton (or, in the case of a button on a UITableViewCell, you can add the app-specific data to a UITableViewCell subclass and access the cell as the button's superview.superview).

gerry3
A: 

If you need to pass more than one argument, you have to create an NSInvocation object. See Apple's reference: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSInvocation%5FClass/Reference/Reference.html

Use the setArgument:atIndex: feature to set your arguments.

Here's a nice introduction on this: http://www.cocoadev.com/index.pl?NSInvocation

Zoran Simic