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];
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];
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).
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:
.
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.
[Class instancesRespondToSelector: @selector (setTo:over:)]
I am not an Objective-C guru, but I remember seeing this code in a book.
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).
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