Zhen
Zhen

Reputation: 12431

How to pass argument in @selector?

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
}

Upvotes: 8

Views: 11250

Answers (3)

Kyle Clegg
Kyle Clegg

Reputation: 39470

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.

Upvotes: 0

Caleb
Caleb

Reputation: 124997

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.

Upvotes: 6

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

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.

Upvotes: 1

Related Questions