1

I'm using this code to show a UIViewController:

CATransition *transition = [CATransition animation];
transition.duration = 0.f;
transition.type = kCATransitionFade;
[self.navigationController.view.layer addAnimation:transition forKey:nil];
[self.navigationController pushViewController:newVC animated:NO];

and the viewController is displayed correctly. Then I'm using this code to prepare an animation (Used to animate the pop):

CATransition *transition = [CATransition animation];
transition.duration = 0.f;
transition.type = kCATransitionFade;
[self.navigationController.view.layer addAnimation:transition forKey:nil];

However, neither of these lines will pop it:

[self.navigationController popViewControllerAnimated:NO];
[self.navigationController popToRootViewControllerAnimated:NO];

What could be the issue? Logging the navigation stack displays both the correct UIViewControllers:

NSLog(@"%@", self.navigationController.viewControllers);
Erik
  • 2,500
  • 6
  • 28
  • 49
  • with out animation try once – Anbu.Karthik Feb 02 '16 at 09:26
  • @Anbu.Karthik same result without animation – Erik Feb 02 '16 at 09:29
  • ya , ok check once your view controllers are added in Navigation stack or not.... – Anbu.Karthik Feb 02 '16 at 09:30
  • Try to add animation to the view controllers view that's being pushed/popped on `viewWillAppear:` and `viewWillDisappear:`. That should work. – Fahri Azimov Feb 02 '16 at 09:31
  • Not really answering your question per se, but if you are trying to implement custom view controller transitions, take a look at the `UIViewControllerAnimatedTransitioning` protocol, as well as this article on objc.io: https://www.objc.io/issues/5-ios7/view-controller-transitions/. – Siyu Song Feb 02 '16 at 09:32
  • Try this.. http://stackoverflow.com/q/34591394/3908884 – Meet Doshi Feb 02 '16 at 11:00
  • @MeetDoshi hmm. It appears the controls on the `UIViewController resets to their default state when I call `[self.navigationController popViewControllerAnimated:NO];` – Erik Feb 02 '16 at 12:42

3 Answers3

2

This is clear,

if you don't push yourViewcontroller, you can't pop yourViewController

if bellow line is missing in your code

 [self.navigationController pushViewController:newVC animated:NO];

none of the both bellow will execute

[self.navigationController popViewControllerAnimated:NO];
[self.navigationController popToRootViewControllerAnimated:NO];
Jamil
  • 2,977
  • 1
  • 13
  • 23
  • Strange, I'm using this line `[self.navigationController pushViewController:newVC animated:NO];` To push my viewController – Erik Feb 02 '16 at 09:42
  • In you 2nd option of code fragment you didi not use '[self.navigationController pushViewController:newVC animated:NO];'. this is the reason you can't pop – Jamil Feb 02 '16 at 10:43
  • Oh, that code isn't when pushing - I added it prior to calling `[self.navigationController popViewControllerAnimated:NO];` to animate the pop. Don't know if this is right though - but I removed it just now – Erik Feb 02 '16 at 10:47
  • It appears the controls on the `UIViewController resets to their default state when I call `[self.navigationController popViewControllerAnimated:NO];` – Erik Feb 02 '16 at 12:42
  • what do you mean by reset to default state. – Jamil Feb 02 '16 at 13:00
  • For instance, I've got a `UISlider` that snaps back to its default value when I try to pop. It's also worth mentioning that this slider is inside a `UITableViewCell` – Erik Feb 02 '16 at 13:01
  • I think I've made a quite obvious mistake here. Let me see – Erik Feb 02 '16 at 13:03
0

Ok, so this is embarrassing.

I was running the push call in the viewDidAppear:animated:, and I had forgotten to implement a check if we had already pushed. So when I popped the new UIViewController, the viewDidAppear:animated re-pushed the new VC again. I also set the it to animate:NO and removed the custom transition, causing the pop and re-push to happen basically instantaneously.

I discovered this by putting a breakpoint at the pop message, and noticed the simulator displayed the root UIViewController for a fraction of a second.

Erik
  • 2,500
  • 6
  • 28
  • 49
0

set your navigationItems left / right / title to nil before

[self.navigationController popViewControllerAnimated:NO];

I saw some thing similar today where the view did not update after pop

David Yang Liu
  • 1,170
  • 2
  • 10
  • 19