0

这就是我执行转换的方式:

extension UIViewController: UIViewControllerTransitioningDelegate {

    public func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        return OverlayPresentationController(presentedViewController: presented, presenting: presenting)
    }

    func presentOverlayController(_ controller: UIViewController) {

        controller.modalPresentationStyle = .custom
        controller.transitioningDelegate = self

        present(controller, animated: true)
    }
} 

然后在我呈现的控制器 ( AlertVC) 中,有时我需要访问它的呈现控制器:

print(presentationController as? OverlayPresentationController) //nil
print(presentationController) //is ok, UIPresentationController

为什么?

介绍:

let controller = AlertVC.instantiate()
controller.update()
presentOverlayController(controller)

class AlertVC: UIViewController {

    class func instantiate() -> AlertVC {
        return UIStoryboard(name: "Alert", bundle: Bundle(for: LoginVC.classForCoder())).instantiateInitialViewController() as! AlertVC
    }

    func update() {
        _ = view

        print(presentationController as? OverlayPresentationController) //nil
    }
}
4

1 回答 1

-3

您可以通过调用 presentingViewController 获得呈现当前视图控制器的视图控制器

// The view controller that presented this view controller (or its farthest ancestor.)
self.presentingViewController 

如果您的呈现视图控制器位于返回 UINavigationController 的导航控制器中。你可以像这样得到你需要的视图控制器:

let presentingNVC = self.presentingViewController as? UINavigationViewController
let neededVC = presentingNVC.viewControllers.last as? NeededViewController
于 2017-07-26T10:06:58.443 回答