AleksandarNS
AleksandarNS

Reputation: 265

remove previous view controller from hierarchy

I am trying skip one ViewController when I press back button from navigation bar.

When I go to next VC it needs to do this:

A -> B -> C

But when I want to go back from C, I want A ViewController to be presented:

C -> A

I managed to do that with next code:

    NSMutableArray *newControllers = [[NSMutableArray alloc]initWithArray:controllers];
    [newControllers removeObjectAtIndex:newControllers.count-2];
    self.navigationController.viewControllers = newControllers;

Problem is navigation bar that is presented. When I get View from A ViewController, navigation bar from B ViewController is presented.

Is there any way that I can remove navigation bar items like I removed view controller from hierarchy?

Upvotes: 2

Views: 1810

Answers (1)

Jay Gajjar
Jay Gajjar

Reputation: 2741

UIViewController *vcPop = nil;  
for (UIViewController *viewContrl in self.navigationController.viewControllers) {
   if ([viewContrl class] == [YOUR POP VIEW CONTROLLER class]) {
       vcPop = (YOUR POP VIEW CONTROLLER  *)viewContrl;
       break;
 }
}
if(vcPop)
   [self.navigationController popToViewController:vcPop animated:YES];

Upvotes: 6

Related Questions