objective-cuitableviewparse-platformseguepfquerytableviewcontrolle

Passing PFObjects from indexPath.row through UIButton in TableViewCell Parse


I have a social app. I have a UIButton inside a TableViewCell. This button is called commentButton. What I want to achieve with this button is for the user to tap it and goto a tableview where the comments for that post are held. The issue is-----is that I am having a super hard time trying to pass the PFObjects at the current indexPath.row through a segue. I have tried multiple things. But let me help you get a better understanding of my problem.

What My Cell Looks Like

As you can see above, it's the comment button that I am trying to initiate the segue on. commentButton has a tag of 222. I have tried changing the class of the button to a NSINTEGER:

cellForRowAtIndexPath

CommentButton *commentButton = (CommentButton*) [cell viewWithTag:222];
commentButton.index = indexPath.row;
[commentButton addTarget:self action:@selector(commentSegue) forControlEvents:UIControlEventTouchUpInside];
commentButton.showsTouchWhenHighlighted = YES;

Calling Segue @Selector

 -(void) commentSegue 
{
   NSLog(@"Button TOUCHED!");
   [self performSegueWithIdentifier:@"showcomments" sender:self];
}

Prepare for Segue

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if([segue.identifier isEqualToString:@"showcomments"])
        {

        CommentsViewController * cvc = segue.destinationViewController;
        if([sender isKindOfClass:[CommentButton class]]){
            CommentButton * button = sender;
            _currentPost =[self.objects objectAtIndex:button.index];
            cvc.postObject = _currentPost;
        }
    }

I have tried multiple things, this current code crashes with an unrecognized selector sent to instance error.

I have also tried:

if([segue.identifier isEqualToString:@"showcomments"]){

        NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
        PFObject *currentObjects = [self.objects objectAtIndex:indexPath.row];
        CommentsViewController * cvc = segue.destinationViewController;
        NSLog(@"Segue Objects: %@", currentObjects);
        cvc.postObject = currentObjects;
    }

But when my tableView is populated my NSLog prints out the same data no matter what cell I choose. It's unable to differentiate which cell im choosing.


Solution

  • You are sending self to performSegueWithIdentifier:sender:. I'm assuming you mean to be sending your button as the sender? (You later try to cast sender as a CommentButton in prepareForSegue:sender:) Try changing this:

    -(void) commentSegue 
    {
       NSLog(@"Button TOUCHED!");
       [self performSegueWithIdentifier:@"showcomments" sender:self];
    }
    

    To this:

    -(void) commentSegueWithButton:(CommentButton *)button
    {
       NSLog(@"Button TOUCHED!");
       [self performSegueWithIdentifier:@"showcomments" sender:button];
    }
    

    Then you need to modify the following:

    [commentButton addTarget:self action:@selector(commentSegue) forControlEvents:UIControlEventTouchUpInside];
    

    To be this:

    [commentButton addTarget:self action:@selector(commentSegueWithButton:) forControlEvents:UIControlEventTouchUpInside];
    

    Also, while you are at it, create a breakpoint for All Exceptions if you don't have that already. That breakpoint will tell you at what line you are getting the unrecognized selector error.