prepareForSegue called before didSelectRowAtIndexP

2020-01-30 07:26发布

I have 3 segues to 3 different views. 2 are implemented with no problem, it is when the third is created that the problems occur.

I have the following didSelectRowAtIndexPath method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@" ----------  did select row");

    if(indexPath.section == 0){
        if(indexPath.row == [self.data count]-1){
            //prior to adding this, everything works
            [self performSegueWithIdentifier:@"MoreComments" sender:self];
        }else{
            [self performSegueWithIdentifier:@"FriendView" sender:friend];
        }
    }else if(indexPath.section == 1){
        if(indexPath.row == [self.data2 count]-1){
            [self performSegueWithIdentifier:@"MorePosts" sender:self];
        }else{
            [self performSegueWithIdentifier:@"FriendView" sender:friend];
        }
    }
}

I have the following prepareForSeque method:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"MorePosts"]){
        MorePostsViewController *mfamvc = segue.destinationViewController;
        mfamvc.data = self.data;
    }else if([segue.identifier isEqualToString:@"FriendView"]){
        FriendViewController *fvc = segue.destinationViewController;
        fvc.friend = friend;
    }else if([segue.identifier isEqualToString:@"MoreComments"]){
          MoreCommentsViewController *mcvc = segue.destinationViewController;
          mcvc.data = self.data2;
    }
}

Before control dragging from my cell to the last view I can see that my program hits didselectrow and then prepareforseque. This makes all the view navigation work perfect.

As soon as I control drag from my cell to the MoreCommentsViewController I start to see the error:

nested push animation can result in corrupted navigation bar Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.

I notice that now also prepareforseque is being called twice, with prepareforseque being called first, then didselectrow, then prepareforsegue again.

What am I doing wrong to conditionally go to these different views?

2条回答
孤傲高冷的网名
2楼-- · 2020-01-30 07:57

You should use either didSelectRowAtIndexPath or segues from cell, but not both. If you want your didSelectRowAtIndexPath to invoke segues, those segues should not be from the cell to the next scene, but rather from the view controller icon in the bar above the scene:

segue between view controllers

You can now select this new segue, go to the "attributes inspector" (option+command+4), and supply a storyboard identifier which you can reference in your code when you call performSegueWithIdentifier.

查看更多
在下西门庆
3楼-- · 2020-01-30 07:57

The reason is you can't drag from a tableview cell to multiple views. As @rdelmar mentioned this is wrong. You should drag from the destination to the source view and then handle manually the way I did above.

Also can be found here: Conditional segue performed on tap on UITableViewCell

查看更多
登录 后发表回答