How to dismiss 2 view controllers in Swift iOS?
Below is my code.
@IBAction func backButtonTapped(sender: AnyObject) {
self.presentingViewController
.presentingViewController
.dismissViewControllerAnimated(true, completion: nil)
}
There's special unwind segue for that purpose, it is intended to roll back to certain view controller in stack.
Let's call the topmost controller (where you go from) as source and the controller in stack (which you want to roll back to top) as destination.
create
IBAction
in destination to be triggered on unwind segue:@IBAction func myUnwindAction(segue: UIStoryboardSegue) {}
it can be empty.
in source controller create an unwind segue by dragging from the controller icon to exit one, it will find the action you created at the step 1. Call the segue
unwind
.now you can issue that segue from code with regular
performSegueWithIdentifier("unwind", sender: nil)
I described how to issue unwind segue from code. For buttons unwind segues can be created in IB directly by dragging a button to exit icon.
Also check this discussion for more info: How to perform Unwind segue programmatically?
Swift 3+ version. You can dismiss two view controllers at a time in Swift 3 with this below code.
func dismissTwoViews() {
self.presentingViewController?
.presentingViewController?.dismiss(animated: true, completion: nil)
}
Swift 4+ version. just we need pop particular view controller use this extension
extension UINavigationController {
func popToViewController(ofClass: AnyClass, animated: Bool = true) {
if let vc = viewControllers.filter({$0.isKind(of: ofClass)}).last {
popToViewController(vc, animated: animated)
}
}
func popViewControllers(viewsToPop: Int, animated: Bool = true) {
if viewControllers.count > viewsToPop {
let vc = viewControllers[viewControllers.count - viewsToPop - 1]
popToViewController(vc, animated: animated)
}
}
}
And use like this in your view controller class
for controller in self.navigationController!.viewControllers as
Array {
if controller.isKind(of:
yourPopControllerName.self) {
self.navigationController?.isNavigationBarHidden = false
_ =
self.navigationController!.popToViewController(controller,
animated: false)
break
}
}
You can only dismiss one view controller at a time. Try this
@IBAction func backButtonTapped(sender: AnyObject) {
self.presentingViewController?.dismissViewControllerAnimated(true, completion: {
let secondPresentingVC = self.presentingViewController?.presentingViewController;
secondPresentingVC?.dismissViewControllerAnimated(true, completion: {});
});
}
Swift 4:
Created an extension for UIViewController that can pop UIViewControllers in NavigationController stack depending in supplied number of times
extension UIViewController {
func pop(numberOfTimes: Int) {
guard let navigationController = navigationController else {
return
}
let viewControllers = navigationController.viewControllers
let index = numberOfTimes + 1
if viewControllers.count >= index {
navigationController.popToViewController(viewControllers[viewControllers.count - index], animated: true)
}
}
}
来源:https://stackoverflow.com/questions/27943607/how-to-dismiss-2-view-controller-in-swift-ios