0

This is how I perform transitioning:

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

And then within my presented controller (AlertVC) at some point I need to access its presentation controller:

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

Why?

Presenting:

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
    }
}
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358

1 Answers1

-3

You get the view controller that is presenting the current view controller by calling presentingViewController

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

If your presenting view controller is in a navigation controller that returns a UINavigationController. You can get the view controller you need like so:

let presentingNVC = self.presentingViewController as? UINavigationViewController
let neededVC = presentingNVC.viewControllers.last as? NeededViewController
Casper Zandbergen
  • 3,419
  • 2
  • 25
  • 49
  • @BartłomiejSemańczyk Where did you try to call `presentingViewController` ? If you try to call it in `viewDidLoad:` it is possible that the value is not set yet. – Casper Zandbergen Jul 26 '17 at 10:13
  • @BartłomiejSemańczyk and are you sure you are calling it in the view controller that you present using `present(controller, animated: true)`? – Casper Zandbergen Jul 26 '17 at 10:16
  • @BartłomiejSemańczyk I think you are missing `presentationController.delegate = self` above the return statement in `presentationController(forPresented:presenting:source:)` – Casper Zandbergen Jul 26 '17 at 10:33
  • @BartłomiejSemańczyk Can you add your init code to the question? – Casper Zandbergen Jul 26 '17 at 10:40