casillas
casillas

Reputation: 16793

Navigation Back shows other Viewcontroller during the transition

I have three Viewcontrollers : ViewControllerA, ViewControllerB and ViewControllerC.

When I am in ViewControllerC, I click on Navigation Back button to go back to ViewControllerA directly, skipping ViewControllerB.

I have tried following approaches, both of them work. But, I wonder while transiting from ViewController C to ViewController A, it shows ViewController B in one second for during the transition.

Is there a way just directly navigate from ViewController C to ViewController A skipping ViewControllerB.

Approach 1:

-(void) viewWillDisappear:(BOOL)animated {
   if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
      NSLog(@"back button pressed");
      [self.navigationController popViewControllerAnimated:YES];
   }
  [super viewWillDisappear:animated];
}

Approach 2:

-(void) viewWillDisappear:(BOOL)animated {
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
        NSLog(@"back button pressed");
        //[self.navigationController popViewControllerAnimated:YES];
        NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
        for (UIViewController *aViewController in allViewControllers) {
            if ([aViewController isKindOfClass:[ViewControllerA class]]) {
                [self.navigationController popToViewController:aViewController animated:NO];
            }
        }
    }
    [super viewWillDisappear:animated];
}

Upvotes: 1

Views: 111

Answers (3)

Priya
Priya

Reputation: 17

        NSArray *arrayViewControllers = [self.navigationController viewControllers];


        for (UIViewController *viewcontroller in arrayViewControllers) {
            if ([viewcontroller isKindOfClass:[ViewControllerA class]]) {
                [self.navigationController popToViewController:viewcontroller animated:true];
            }
        }

Upvotes: 0

Reinier Melian
Reinier Melian

Reputation: 20804

You need to use setViewControllers method, and pass only the viewControllerA that is the first element in your navigationController.viewControllers array

Code

- (IBAction)backAction:(id)sender {
    UIViewController * viewControllerA = [self.navigationController.viewControllers firstObject]; //we get the first viewController here
    [self.navigationController setViewControllers:@[viewControllerA] animated:YES];
}

similar answer here How to start from a non-initial NavigationController scene but in swift

enter image description here

Upvotes: 1

McDonal_11
McDonal_11

Reputation: 4075

Try popToRootViewControllerAnimated . It will move to First ViewController which NavigationController embed.

[self.navigationController popToRootViewControllerAnimated:YES];

Upvotes: 0

Related Questions