Giorgio Doganiero
Giorgio Doganiero

Reputation: 143

How to correctly dismiss a modal View Controller?

Inside my Custom TabBarController I set the NavigationController for each tab

let viewController = [HomeViewController(), ProfileViewController()]
let navControllers = viewControllers.map { return UINavigationController(rootViewController: $0) }
setViewControllers(navControllers, animated: false)

In my HomeViewController I have a CollectionView that when selecting a cell it pushes my VideoPlayerViewController to the NavigationController. The VideoPlayerViewController then presents a ViewController as a modal:

class PopoverViewController: UIViewController {
    @IBAction func presentPopover() {
        let popoverViewController = PopoverViewController()
        popoverViewController.modalPresentationStyle = .fullScreen
        popoverViewController.delegate = self
        present(popoverViewController, animated: true, completion: nil)
    }
}

In my PopoverViewController I have a close button that calls the delegate method in the VideoPlayerViewController:

class PopoverViewController: UIViewController {
    @objc func closeTapped() {
        delegate?.willClose(self)
    }
}

extension VideoPlayerViewController: PopoverViewControllerDelegate {
    func willClose(_ viewController: PopoverViewController) {
        viewController.dismiss(animated: true, completion: nil)
    }
}

When calling dismiss on my PopoverViewController it not only dismisses the modal ViewController but it also pops the VideoPlayerViewController from the NavigationController and it returns to the rootViewController (HomeViewController).

I want to go back to the VideoPlayerViewController and only dismiss the PopoverViewController without popping the VideoPlayerViewController.

Upvotes: 3

Views: 2845

Answers (1)

πter
πter

Reputation: 2217

You are calling the dismiss on your VideoPlayerViewController. This should be done on the PopoverViewController as below:

class PopoverViewController: UIViewController {
    @objc func closeTapped() {
        dismiss(animated: true, completion: nil)
    }
}

Upvotes: 2

Related Questions