Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly dismiss a modal View Controller?

Tags:

ios

swift

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.

like image 437
Giorgio Doganiero Avatar asked Dec 22 '25 15:12

Giorgio Doganiero


1 Answers

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)
    }
}
like image 129
πter Avatar answered Dec 24 '25 04:12

πter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!