How to dismiss 2 view controller in swift ios? [closed]

邮差的信 提交于 2019-12-21 07:56:29

问题


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)
}

回答1:


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
                                }
                            }



回答2:


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.

  1. create IBAction in destination to be triggered on unwind segue:

    @IBAction func myUnwindAction(segue: UIStoryboardSegue) {}

it can be empty.

  1. 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.

  2. 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?




回答3:


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: {});
        });
}



回答4:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!