Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling popViewControllerAnimated twice

I've got a UINavigationController with a series of UIViewControllers on it. Under some circumstances, I want to pop back exactly two levels. I thought I could do it by calling popViewControllerAnimated twice in a row, but it turns out that the second time I call it, it's not popping anything and instead returning NULL. Do I need to store a reference to my destination VC and call popToViewControllerAnimated instead? I can do that, but it complicates my code since I'd have to pass the UIViewController* around as I'm pushing VCs onto the stack.

Here's the relevant snippet:

UIViewController* one = [self.navigationController popViewControllerAnimated:YES]; if (...) {     // pop twice if we were doing XYZ     UIViewController *two = [self.navigationController popViewControllerAnimated:YES];     // stored in "one" and "two" for debugging, "two" is always 0 here. } 

Am I doing something weird here? I want to write idiomatic code, so if the "right" way is to call popToViewControllerAnimated, or something else entirely, I'll happily change it.

like image 933
Mike Kale Avatar asked Jul 14 '09 01:07

Mike Kale


People also ask

How do I pop two viewControllers at a time?

There's also a setViewControllers(_:animated:) to include the pop animation. Alternatively you could find the second last view controller in the viewControllers array and then use popToViewController to avoid overwriting the entire view controller stack.

What does popViewController do?

Pushes a view controller onto the receiver's stack and updates the display. func popToRootViewController(animated: Bool) -> [UIViewController]? Pops all the view controllers on the stack except the root view controller and updates the display.

How do I pop navigation controller in Swift?

You can do it by selecting the View Controller in Storyboard editor and clicking Editor -> Embed In -> Navigation Controller. Also make sure that you have your Storyboard Entry Point (the arrow that indicates which view controller is presented first) either pointing to Navigation Controller or before it.


2 Answers

In this case you would need to pop back to a specific viewcontroller in the navigationController like so:

[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:2] animated:YES]; 

That code would pop to the third viewcontroller on the navigationController's stack.

like image 107
Ben Harris Avatar answered Sep 25 '22 01:09

Ben Harris


I think its better to count the number of view controllers in you stack and then subtract the number of view controllers you would like to pop.

 NSInteger noOfViewControllers = [self.navigationController.viewControllers count];  [self.navigationController   popToViewController:[self.navigationController.viewControllers   objectAtIndex:(noOfViewControllers-2)] animated:YES]; 

With this solution you wont mess up the pop if you add a new view to your project later.

like image 25
Flatron Avatar answered Sep 25 '22 01:09

Flatron