I\'m trying to perform a segue using a UIButton
, which is located within in a custom UITableViewCell
class called GFHomeCell
.
The
Well, one answer would be to just go up a level in the view hierarchy:
- (void)commentButtonClick:(id)sender {
GFHomeCell * cell = (GFHomeCell *) [(UIButton*)sender superview];
if (cell && [cell Class] == [GFHomeCell class]) {
//do whatever with cell.postID
[self performSegueWithIdentifier:@"addCommentSegue" sender:sender];
}
}
Oh, I forget... you may have to go up two levels to get past the contentView property:
GFHomeCell * cell = (GFHomeCell *) [[(UIButton*)sender superview] superview];
There are basically two common approaches to this situation. One is to search up through the button's superviews until you find the cell. You shouldn't rely on going up one or two levels, because the hierarchy has changed in the past, and may change again (you need to go up two levels in iOS 6, but 3 in iOS 7). You can do it like this,
-(void)commentButtonClick:(UIButton *) sender {
id superView = sender.superview;
while (superView && ![superView isKindOfClass:[UITableViewCell class]]) {
superView = [superView superview];
}
[self performSegueWithIdentifier:@"addCommentSegue" sender:superView];
}
The other way is to assign a tag to your button in cellForRowAtIndexPath: equal to the indexPath.row (if you only have one section), and then use sender.tag to get the indexPath of the cell that contained the tapped button.