Reputation: 43
I have something like this in my Storyboard: NavigationController -> A -> B -> C where A, B and C are ViewControllers. I am staying in C where I have variable representing B controller and doing something like this:
controllerB?.navigationController?.popViewController(animated: true)
self.navigationController?.popViewController(animated: true)
Which should dismiss B controller and then C. But when I do this in simulator, I see C controller beeing dismissed first and after it I see B controller for a while before it is dismissed too. I tried delay like this:
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0)
But it is not working either Can you help me? And is it even possible to dissmiss B before C?
Upvotes: 0
Views: 287
Reputation: 131481
You can't call pop twice in a row like you're trying to do, and using timers is fragile and not recommended. (It's a shame the pop methods don't take a completion handler like the modal dismiss methods do.)
Just use popToViewController(_:animated:)
. Send the message to self.navigationController
, and send A as the view controller to pop to.
If A is the root, just use self.navigationController.popToRootViewController(animated:true)
Upvotes: 0
Reputation: 3402
Why not just pop both of them at the same time? You can write an extension for navigation controller and pop back to a passed in viewcontroller regardless of how many vc's are pushed on top.
extension UINavigationController {
func popBackTo(viewcontroller: UIViewController.Type, animated: Bool) {
for vc in self.viewControllers {
if vc.isKind(of: viewcontroller) {
self.popToViewController(vc, animated: animated)
}
}
}
}
Use case:
self.navigationController?.popBackTo(viewcontroller: ViewControllerA.self, animated: true)
Upvotes: 1