I\'m presenting a ViewController modally. How can I access the parent view controller ?
My architecture is TabBarController=>VC1=>VC2=>VC3=>MVC1, and I want to reach VC3
You can always go further back just by calling parentViewController like so:
self.parentViewController.parentViewController
.... and so on until you reach the right one.
Addition to Vibhor Goyal - Sometimes, self.presentingViewController registers as NavigationController. Therefore try:
if let vc = self.presentingViewController.childViewControllers.last as? YourViewController {
// do stuff here
}
Here I came up with universal method to navigate from any place to root.
You create a new Class file with this class, so that it's accessible from anywhere in your project:
import UIKit
class SharedControllers
{
static func navigateToRoot(viewController: UIViewController)
{
var nc = viewController.navigationController
// If this is a normal view with NavigationController, then we just pop to root.
if nc != nil
{
nc?.popToRootViewControllerAnimated(true)
return
}
// Most likely we are in Modal view, so we will need to search for a view with NavigationController.
let vc = viewController.presentingViewController
if nc == nil
{
nc = viewController.presentingViewController?.navigationController
}
if nc == nil
{
nc = viewController.parentViewController?.navigationController
}
if vc is UINavigationController && nc == nil
{
nc = vc as? UINavigationController
}
if nc != nil
{
viewController.dismissViewControllerAnimated(false, completion:
{
nc?.popToRootViewControllerAnimated(true)
})
}
}
}
Usage from anywhere in your project:
{
...
SharedControllers.navigateToRoot(self)
...
}
You can access parent by calling:
self.presentingViewController
As per apple documentation:
The view controller that presented this view controller (or its farthest ancestor.)
//You have to get the root, iterate through the root's ViewControllers and then ask for the ParentViewController.
UINavigationController *root = (UINavigationController*)[[(AppDelegate*) [[UIApplication sharedApplication]delegate] window] rootViewController];
for (UIViewController *VC in root.viewControllers) {
if([VC isKindOfClass:[YourParentViewController class]]){
YourParentViewController* parent = (YourParentViewController*)VC;
[parent callMethod]; // your code here
[self dismissViewControllerAnimated:YES completion:nil];
}
}
The way I'd go about something like this is to simply create a delegate. In AskPasswordViewController
's header, put
id delegate;
and
@property (nonatomic, assign) id delegate;
Synthesize it in the implementation file. Then after you alloc/init the modal controller, and before you present it, set modalViewController.delegate = self;
. Then within the modal controller, you can call self.delegate
to get information from the view controller that presented it. I hope this helps