简体   繁体   中英

Navigate to root view-controller from modally presented view controller

I have the below scenario.

Root viewcontroller - A

pushed viewcontroller - B

Presented viewcontroller - C

A -> pushed B.

B -> presented C.

How can i go back to A from C.

You can write following inside C

let presenting = self.presentingViewController ?? self.navigationController?.presentingViewController
let navCtrl1 = presenting as? UINavigationController // in case you presented C using b.navigationController.present...
let navCtrl2 = presenting?.navigationController // in case you presented c using b.present...
if let navCtrl = navCtrl1 ?? navCtrl2  {
    self.dismiss(animated: true, completion: {
        navCtrl.popToRootViewController(animated: true)
    })
}

UPDATE

I would like to know if is there anyway to bypass the dismiss and direct pop to root view controller. I want to avoid showing View-controller B

let presenting = self.presentingViewController ?? self.navigationController?.presentingViewController
let navCtrl1 = presenting as? UINavigationController // in case you presented C using b.navigationController.present...
let navCtrl2 = presenting?.navigationController // in case you presented c using b.present...
if let navCtrl = navCtrl1 ?? navCtrl2  {
    navCtrl.popToRootViewController(animated: false)
    self.dismiss(animated: true, completion: nil)
}

How?

To achieve this, you will have to pass the presenting controllers UINavigationController as a variable to the View controller you are present -ing. Let me show you how, and the result.

Pushing from vcA to vcB is fairly straight forward. One thing to note is that when you push from vcA to vcB , vcA will be in the Navigation stack. Having that in mind, let me move one.

First make changes to vcC by adding a variable to hold the UINavigationCongroller of the view controller that presented vcC , which would be vcB . Do as follows ( READ THE COMMENTS )

class ViewControllerC: UIViewController {

    // Variable that holds reference to presenting ViewController's Navigtion controller
    var presentingNavigationController: UINavigationController!

    //Some action that triggers the "Go-back-to-A"
    @objc func pressed() {
        // When the completion block is executed in dismiss,
        // This function will loop through all ViewControllers in the presenting Navigation stack to see if vcA exists
        // Since vcA was earlier pushed to the navigation stack it should exist
        // So we can use the same navigation controller to pop to vcA    
        // Set the animated property to false to make the transition instant
        dismiss(animated: false) {
            self.presentingNavigationController.viewControllers.forEach({
                if let vc = $0 as? ViewController {
                    self.presentingNavigationController.popToViewController(vc, animated: true)
                }
            })
        }
    }

And in vcB you can add the following to present(_:_:_:) function

// function call
@objc func pressed() {
    let vc = ViewControllerC()
    // Setting the navigation controller for reference in the presented controller
    vc.presentingNavigationController = self.navigationController  
    present(vc, animated: true, completion: nil)
}

The results

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM