How do I pop the view controller underneath a pushed view controller?

后端 未结 4 716
清酒与你
清酒与你 2021-02-06 01:36

I want to push a view controller onto the stack, then pop the first one that pushed the new one.

-(void) someMethod {
    MegaSuperAwesomeViewController *tempVC          


        
4条回答
  •  不思量自难忘°
    2021-02-06 02:08

    I had trouble figuring this out also so I wanted to share how I got this to work.

    Let's say you have a stack of VCs VC1 being the root then you push VC2 and from VC2 you want to push VC3 but once pushed you don't want the user to go back to VC2 but rather to VC1 (the root). The way to do that is:

    //push VC3 from VC2
    [[self navigationController] pushViewController:VC3 animated:YES];
    
    // now remove VC2 from the view controllers array so we will jump straight back to VC1
    NSMutableArray *viewHeirarchy =[[NSMutableArray alloc] initWithArray:[self.navigationController viewControllers]];
    [viewHeirarchy removeObject:self];
    self.navigationController.viewControllers = viewHeirarchy;
    

    Hope this helps someone else

提交回复
热议问题