Search code examples
objective-ciospushviewcontrollerpoptoviewcontroller

How to push a view controller onto navigation stack AFTER executing popToRootViewController


I am trying to push a new view controller onto a navigation stack only after I pop the stack to rootviewcontroller

//Select the tab I want to go to
self.tabBarController.selectedIndex = FEEDPAGE_INDEX;

//Retrieve the navcon in the feed page
UINavigationController *navcon = (UINavigationController*)[self.tabBarController.viewControllers objectAtIndex:FEEDTAB_INDEX];

//Pop to root view controller here
[navcon popToRootViewControllerAnimated:YES];

//Push a new root view controller onto stack
QuestionAnswerViewController *x = [[QuestionAnswerViewController alloc]init];
[navcon pushViewController:x animated:YES];

The push didn't work i.e. no new page was displayed. It seemed that the popToRootViewController causes this to happen (not sure exactly). Any advise on how I can pop and then push a new page?


Solution

  • QuestionAnswerViewController *x = [[QuestionAnswerViewController alloc]init];
    UINavigationController *navcon = (UINavigationController*)[self.tabBarController.viewControllers objectAtIndex:FEEDTAB_INDEX];
    NSArray *arr = [navcon viewControllers];
    NSArray *newStack = [NSArray arrayWithObjects:[arr objectAtIndex:0], x, nil];
    [navcon setViewControllers:newStack];