iosswiftuikit

How to move between view controllers?


I have first view controller with navigation bar. Next I go to second view controller with navigation bar. And next I want to go to third view controller with navigation bar and overCurrentContext options. And in third view controller I want to my back button return to first view controller. I use this code to do it:

In second vc:

let detailViewController = ThirdVC()
self.navigationController?.modalPresentationStyle = .overFullScreen
self.navigationController?.pushViewController(detailViewController, animated: false)

In third vc for back button:

self.navigationController?.popToRootViewController(animated: false)

But I can't see second vc as a background of third vc. .overCurrentContext doesn't work. I see gray background. How to fix it?


Solution

  • In your thirdVC, declare a closure:

    var onPopToRoot:(()-> Void)?
    

    then in your pop function, for example:

    @objc private func popToRoot() {
          onPopToRoot?()
          dismiss(animated: true)
    }
    

    In your secondVC:

    @objc private func goThird() {
            let detailViewController = ThirdVC()
            self.navigationController?.modalPresentationStyle = .overFullScreen
            detailViewController.onPopToRoot = {[weak self] in
                self?.navigationController?.popToRootViewController(animated: true)
            }
            self.present(detailViewController, animated: true, completion: nil)
        }