How to pass argument in @selector?

11,233

Solution 1

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.

Solution 2

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.

Share:
11,233
Zhen
Author by

Zhen

Updated on June 09, 2022

Comments

  • Zhen
    Zhen almost 2 years

    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
    }
    
  • Zhen
    Zhen almost 13 years
    I have a button on every cell. How can I do this?
  • Caleb
    Caleb almost 13 years
    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.