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.
精彩评论