0

I want to close my current view controller and go to the next page?

i.e close the current view controller and push another view controller on the stack.

I am able to close current page using the below code

[[self navigationController] popViewControllerAnimated: YES];

But opening a new controller just after this is not working.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Vijay
  • 13
  • 1
  • 5
  • Push is not working as self is not there anymore, as you have already popped the self. – rishi Jul 10 '14 at 17:05
  • If you are using a navigation controller, just push a new view on the stack. – JerryCrowley Jul 10 '14 at 17:06
  • Why are you trying to pop then push? If you're going to that then reset the rootViewController. A push implies you want a navigation stack. Popping it means you want to remove the VC from memory. These are fundamentally opposite. If you want to keep a navigation stack, then just don't pop. – LyricalPanda Jul 10 '14 at 17:07

2 Answers2

5

Calling push and pop on the self at the same time will not work, as self has already been removed. You need to handle the stack of view controller -

 UINavigationController *navigationController = self.navigationController;

 NSMutableArray *activeViewControllers=[[NSMutableArray alloc] initWithArray: navigationController.viewControllers] ;
 [activeViewControllers removeLastObject];

 // Reset the navigation stack
 [navigationController setViewControllers:activeViewControllers];

 [navigationController pushViewController:yourViewController animated:YES];

Apart from this if you don't want this view controller , when popping from your new view controller, you have other options available.

Like firstVC -> secondVC -> thirdVC

Now from thirdVC if you directly want to come to firstVC (skipping secondVC, i believe which is why you want both push and pop). Then for this purpose you can use - popToViewController.

[self.navigationController popToViewController:firstVC animated:YES];

Just checked that there are already couple of questions available on the same topic, you can refer those too for good opinion and answers-

How can I pop a view from a UINavigationController and replace it with another in one operation?

popping and pushing view controllers in same action

How to pop a viewController and then Push a viewController through delegation

Community
  • 1
  • 1
rishi
  • 11,779
  • 4
  • 40
  • 59
  • 1
    This may be a good place to do a quick education on the difference between push and pop, and why it doesn't make sense to try to do both – LyricalPanda Jul 10 '14 at 17:09
1

You can do this :

[CATransaction begin];
[CATransaction setCompletionBlock:^{
    [self.navigationController pushViewController:viewController animated:YES];
}];

[self.navigationController popToViewController:firstVC animated:YES];

[CATransaction commit];

This will pop to 'firstVC' and push the new viewController as soon as the pop animation is finished.

However, I doubt this behavior will be very user-friendly, because your are popping and pushing right after each other ... very confusing. But if it is suitable in your case, you can sure do it using the above code :)

CodingMeSwiftly
  • 3,231
  • 21
  • 33