开发者

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
}


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜