简体   繁体   中英

Why is my navigation stack mixed up after unwind segue?

I have a navigation route that looks like this:

在此处输入图片说明

A is a UITableViewController embedded in a tab bar controller. Tapping one of the rows segues to B which is a view controller embedded in a navigation controller. Tapping the pink button on B pushes view controller C , and taping "Recent Songs" segues to D . Finally, tapping a song on D runs the method uploadSong()

-(void) uploadSong {
    ViewControllerB *viewControllerB = [self.storyboard instantiateViewControllerWithIdentifier:@"viewControllerB"];
    ViewControllerA *viewControllerA = [self.storyboard instantiateViewControllerWithIdentifier:@"viewControllerA"];

    NSArray *vcArray = @[self,viewControllerA,viewControllerB];
    [self.navigationController setViewControllers:vcArray animated:YES];
    [self performSegueWithIdentifier:@"unwindToViewControllerB" sender:self];
}

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

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

        ViewControllerB *viewControllerB = segue.destinationViewController;
        viewControllerB.object = self.friendObject;
        viewControllerB.spotifyDismiss = YES;
        viewControllerB.hidesBottomBarWhenPushed = YES;
    }
}

All goes well, and I end up back at B with the navigation controller's back button taking me back to A as I wanted. However, I find that if I instead tap on the tab bar item (bottom left) it shows me D again instead of doing what is common in apps and taking you to the first view controller in the stack, which in this case would be A .

So my question is, why after my unwind segue does my back button from B take me to A , but tapping the tab bar item takes me to D instead of A ?

It would seem that the fix was simply putting the view controllers in vcArray in the obvious correct order [viewControllerA, viewControllerB, self] instead of [self, viewControllerA, viewControllerB] . Now my navigation stack acts exactly as it should whether I tap the back button or the tab bar item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM