How to pop back to root view controller but then push to a different view? How to pop back to root view controller but then push to a different view? xcode xcode

How to pop back to root view controller but then push to a different view?


If I understand you correctly, you have a stack of view controllers:

A (root) - B - C - D - E

And you want it to become:

A (root) - F

Right? In that case:

NSArray *viewControllers = self.navigationController.viewControllers;NSMutableArray *newViewControllers = [NSMutableArray array];// preserve the root view controller[newViewControllers addObject:[viewControllers objectAtIndex:0]];// add the new view controller[newViewControllers addObject:viewThreadController];// animatedly change the navigation stack[self.navigationController setViewControllers:newViewControllers animated:YES];

Swift 4

// get current view controllers in stack and replace themlet viewControllers = self.navigationController!.viewControllerslet newViewControllers = NSMutableArray()// preserve the root view controllernewViewControllers.add(viewControllers[0])// add the new view controllernewViewControllers.add(viewThreadController)// animatedly change the navigation stackself.navigationController?.setViewControllers(newViewControllers as! [UIViewController], animated: true)


I think
[self.navigationController pushViewController:viewThreadController animated:YES]; is using a different NavigationController than the statement before that.Because after popping to the root view Controller you loose the navigation Controller you are in. Solve that using this code instead

UINavigationController *nav = self.navigationController; [self.navigationController popToRootViewControllerAnimated:NO];[nav pushViewController:viewThreadController animated:YES];

I also think that this wont solve your whole problem. You will probably get an error saying that two fast popping and pushing may invalidate the NavigationController.
And to solve that you can either push the NavigationController in the viewDidDissappear Method of the 2nd View Controller or push it in the viewDidAppear Method in the Main View Controller(item listing).


An easy way to accomplish what you want to do is to build some simple logic into your main root view controllers -(void)viewWillAppear method and use a delegate callback to flip the logic switch. basically a "back reference" to the root controller. here is a quick example.

main root controller (consider this controller a) - well call it controllerAset a property to keep track of the jump status

@property (nonatomic) BOOL jumpNeeded;

setup some logic in

- (void)viewWillAppear:(BOOL)animated {    [super viewWillAppear:animated];    self.jumpNeeded ? NSLog(@"jump needed") : NSLog(@"no jump needed");    if (self.jumpNeeded) {        NSLog(@"jumping");        self.jumpNeeded = NO;        [self performSegueWithIdentifier:@"controllerC" sender:self];    }   }

Now, in your main root controller,when a tableview row is selected do something like thiswhen pushing to controllerB in your tableView did select method

[self performSegueWithIdentifer@"controllerB" sender:self];

then implement your prepare for segue method

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {  //setup controller B  if([segue.identifier isEqualTo:@"controllerB"]){    ControllerB *b = segue.destinationViewController;    b.delegate = self;  //note this is the back reference  }  //implement controller c here if needed}

Now move on to controllerByou need to set a property called "delegate" to hold the back referenceand you need to import the header file from the root controller

#import "controllerA"@property (nonatomic,weak) controllerA *delegate;

then just before you pop back to controllerA, you set the flag

   self.delegate.jumpNeeded = YES;    [self.navigationController popViewControllerAnimated:YES];

and that is about it. You don't have to do anything with controllerC. There are a few other ways to do, but this is pretty straight forward for your needs. hope it works out for you.