prepareForSegue called before didSelectRowAtIndexPath only when third segue is added

柔情痞子 提交于 2019-11-26 22:48:00

问题


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?


回答1:


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:

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.




回答2:


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



来源:https://stackoverflow.com/questions/18165684/prepareforsegue-called-before-didselectrowatindexpath-only-when-third-segue-is-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!