2

I have a UINavigationController with the following view controllers

A -> B (B is on top)

Now for some action in B, I want to replace B with C (finally it should be A-> C).

I tried the following: when action occurs in B, I do a [self.navigationController popViewControllerAnimated:NO];. In the viewWillAppear function of A, I check if I need to push C immediately and do so.

The problem is that when C is pushed, the transition animation is a pop-animation (slides from right to left) instead of push.
Looks like the animation applied is getting confused with the earlier pop.

How do I fix this ?

I tried passing YES in the [self.navigationController popViewControllerAnimated:NO]; above but that didn't solve the problem neither

Mel
  • 5,837
  • 10
  • 37
  • 42
naiveCoder
  • 1,009
  • 4
  • 16
  • 28

1 Answers1

10

This should work:

NSMutableArray *vcs = [[self.navigationController viewControllers] mutableCopy];
NSUInteger lastVcIndex = [vcs count] - 1;
if (lastVcIndex > 0) {
    [vcs replaceObjectAtIndex:lastVcIndex withObject:viewControllerC];
    [self.navigationController setViewControllers:vcs animated:YES];
}
lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • thanks for the input .. so, i need to have this code in B right ? for some reason, i dont have access to C in B and want to carry out the action in A itself ... i need to carry some pre-setup before i load and that i need to do that in A itself .. any way to carry this out in A itself ? thanks. – naiveCoder Mar 27 '12 at 19:45
  • Sure, you can use this code from A as well. You need to invoke a function in A or send a message to A from B when you want it to do it. – lnafziger Mar 27 '12 at 19:54
  • great , i will check that and come back. – naiveCoder Mar 28 '12 at 10:42
  • Is the conditional really needed? Since `lastVCIndex` > 0 for any view but the root, you can obviate the need for it by simply omitting this code from the root viewController. Anyway I've included another solution as well [here](http://stackoverflow.com/a/17328244/1431728). – JohnK Jun 26 '13 at 18:57
  • @JohnK: Well, someone could always call this on the root view controller, in which case we want it to be a no-op... It could also be a problem if someone wasn't using a navigation controller and tried to use this code. This makes it no-op instead of crash. – lnafziger Jun 27 '13 at 04:06