如何从提供的控制器中访问我的自定义UIPresentationController?

时间:2017-07-26 09:40:48

标签: ios swift uipresentationcontroller

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

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
    }
}

1 个答案:

答案 0 :(得分:-3)

通过调用presentsViewController

,您将获得呈现当前视图控制器的视图控制器
// 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