8

How can I pass an argument in the @selector for my code below?

[thisIconBtn addTarget:self action:@selector(changeIconState) forControlEvents:UIControlEventTouchUpInside];

-(void)changeIconState:(UITableViewCell*)thisCell
{
  //do something
}
Keavon
  • 6,837
  • 9
  • 51
  • 79
Zhen
  • 12,361
  • 38
  • 122
  • 199
  • Theres a way you can call a selector with multiple arguments, and this question was already answered... you can read it [here](http://stackoverflow.com/questions/1018195/objective-c-calling-selectors-with-multiple-arguments) – iruleonu Jun 05 '11 at 12:18

3 Answers3

6

First, the colon is part of the selector: @selector(changeIconState:).

Second, actions are methods that take a particular set of parameters — you can't just use any method as an action. Usually, actions look like this:

- (void)myAction:(id)sender;

where sender is a pointer to the object that's sending the action. In your code, when thisIconButton is tapped, that button would be passed as the sender.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • I have a button on every cell. How can I do this? – Zhen Jun 05 '11 at 12:31
  • Depends on how you set things up. You can examine the button's superview to figure out which cell it's in. Or, you can have the button target the cell instead of the view controller. The cell's action could then call your `-changeIconState:` method. – Caleb Jun 05 '11 at 12:40
1

If you want the cell to which the button belongs, get it using button.superview.superview but I don't think you can alter the arguments of target methods for control events.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
0

One of the most common reasons for trying to send data through a selector is when you use a custom button in a UITableViewCell.

Matthias Bauch provided an excellent code sample on how to get the indexPath by looking up the cell on the sender in a related post. See https://stackoverflow.com/a/5690329/654870.

Community
  • 1
  • 1
Kyle Clegg
  • 38,547
  • 26
  • 130
  • 141