I have a view controller (containing my menu) presented on top of another view controller (my application).
I would need to access the presenting view controller (below my menu) from the presented view controller (my menu), for example to access some variables or make the presenting view controller perform one of its segues.
However, I just can't figure out how to do it. I'm aware of the "presentingViewController" and "presentedViewController" variables but I didn't manage to use them successfully.
Any Idea ?
Code (from the presented VC, which as a reference to the AppDelegate in which the window is referenced) :
if let presentingViewController = self.appDelegate.window?.rootViewController?.presentingViewController {
presentingViewController.performSegue(withIdentifier: "nameOfMySegue", sender: self)
}
Here is a use of the delegation Design pattern to talk back to the presenting view controller.
First Declare a protocol, that list out all the variables and methods a delegate is expected to respond to.
Next : Make your presenting view controller conform to the protocol. Set your presenting VC as the delegate
Finally, when you are ready to call a method on the presenting VC (Delegate).
Assuming that VCApplication is presenting VCMenu, in VCMenu you can access VCApplication with:
Your example
self.appDelegate.window?.rootViewController?.presentingViewController
is looking for the ViewController that presented therootViewController
- it will benil
.EDIT Per TheAppMentor I've added
weak
so there are no retain cycles.