如何正确消除模态视图控制器?

时间:2019-09-23 18:21:50

标签: ios swift

在我的自定义TabBarController内部,我为每个标签设置了NavigationController

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

在我的HomeViewController中,我有一个CollectionView,当选择一个单元格时,它将我的VideoPlayerViewController推送到NavigationController。然后VideoPlayerViewController将ViewController呈现为模态:

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

在我的PopoverViewController中,有一个关闭按钮,该按钮在VideoPlayerViewController中调用委托方法:

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

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

在我的PopoverViewController上调用dismiss时,它不仅关闭模态ViewController,而且还从NavigationController弹出VideoPlayerViewController,并返回到rootViewController(HomeViewController)。

我想回到VideoPlayerViewController,只关闭PopoverViewController而不弹出VideoPlayerViewController。

1 个答案:

答案 0 :(得分:0)

您正在调用VideoPlayerViewController上的解雇程序。这应该在PopoverViewController上完成,如下所示:

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