How to Pop Two Views at Once from a Navigation Controller

How do I pop two views at once from a navigation controller?

You can try this to Jump between the navigation controller stack as well

NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
for (UIViewController *aViewController in allViewControllers) {
if ([aViewController isKindOfClass:[RequiredViewController class]]) {
[self.navigationController popToViewController:aViewController animated:NO];
}
}

Pop 2 view controllers in Nav Controller in Swift

Expanding on my comment, find the second last view controller in the viewControllers array and then use popToViewController to avoid overwriting the entire view controller stack.

Example (assumes the navigation controller has more than 1 view controller):

func backTwo() {
let viewControllers: [UIViewController] = self.navigationController!.viewControllers as [UIViewController]
self.navigationController!.popToViewController(viewControllers[viewControllers.count - 3], animated: true)
}

Objective-C

NSArray *viewControllers = [self.navigationController viewControllers];
[self.navigationController popToViewController:viewControllers[viewControllers.count - 3] animated:YES];

How to pop VC to jump previous two View Controllers using Swift Language?

You are popping (going back to previous controller) instead of pushing (adding new) view controller. What you should do is:

@IBAction func actSingUp(_ sender: Any) {

let storyboard = UIStoryboard(name: "LoginSB", bundle: nil)
let signupvc = storyboard.instantiateViewController(withIdentifier: "loginVC") as! LoginVC

// this step is optional, it will remove SignUp controller from navigation stack
navigationController?.popViewController(animated: false)

// present login controller
navigationController?.pushViewController(signupvc, animated: true)
}

Swift & Navigation : How do I pop until a certain ViewController?

The navigation controller holds a group of view controller and you can pop to any sub-controller you want. For example your flow seem like:

HomeViewController -> ContactViewController -> ContactDetailsViewController -> ChatViewController

Then from ChatViewController you want to push back to ContactViewController:

class ChatViewController: UIViewController {
....
func popToContact() {
if let contactVC = self.navigationController?.viewControllers.filter { $0 is ContactViewController }.first {
self.navigationController?.popToViewController(contactVC, animated: true)
}
}
}

Pop a navigation controller

In your VC2, Change to use this code

func stopTapped() {
print("pop it")
self.dismiss(animated: true, completion: nil)
}

Pop to another view controller in navigation controller hierarchy

I asked it some time ago and now I became smarter so I can answer my own question in a really right way.

I had to use modal view controller. Thus when we want to create a new conversation we're showing the third VC as a modal view controller, and when the conversation is created we're delegating about this to the first VC. In the first view controller I'm creating the second VC and pushing it after that I dismissed the modal VC.

NewConversationViewController *vc = [NewConversationViewController new]; //any setups
[self.navigationController pushViewController:vc animated:NO];
[self dismissViewControllerAnimated:modalVC completion:nil];

Is it possible to pop the UINavigationController twice?]

There is a few pop options

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
  • The first pops the top controller.
  • The second allows you to pop the whole stack off to get to the root.
  • The third allows you to pop to any viewController you have a reference to. You can get the viewController with self.navigationController.viewControllers and then work with the array to get the specific viewController you want to pop to

How can I pop specific View Controller in Swift

Try following code:

for controller in self.navigationController!.viewControllers as Array {
if controller.isKind(of: ViewController.self) {
self.navigationController!.popToViewController(controller, animated: true)
break
}
}


Related Topics



Leave a reply



Submit