Popview after dismiss modal view controller in swift

2.4k views Asked by At

I have a navigation controller A on which i push the view controller B. From B i present modally the view controller C. After I dismiss C, I tried back to A. Therefore the Navigation flow is A->B -> (present ModalView) -> C. I tried without success this code in B:

 self.navigationController?.popToRootViewControllerAnimated(true)

Any suggestion on how can i achieve this?

This case just happen in iOS7

Thank you

1

There are 1 answers

1
lukszar On

You have to dismiss modal view controller (C) and popToRootViewController in NavigationController. Try the code below in C View Controller:

    let presentingVC = self.presentingViewController!
    let navigationController = presentingVC is UINavigationController ? presentingVC as? UINavigationController : presentingVC.navigationController
    navigationController?.popToRootViewControllerAnimated(false)

    self.dismissViewControllerAnimated(true, completion: nil)

In this case user will see just dismissing modal view controller. Pop to Root View Controller in Navigation Controller will be made in backgroud.

Another option is dismissing Modal View Controller and after that pop to the Root View Controller with animation, then user will see everything. Code for that below:

    let presentingVC = self.presentingViewController!
    let navigationController = presentingVC is UINavigationController ? presentingVC as? UINavigationController : presentingVC.navigationController


    self.dismissViewControllerAnimated(true, completion: { () -> Void in

        navigationController?.popToRootViewControllerAnimated(true)
        return
    })