Access modal view controller parent

Viewed 32798

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 from MVC1.

In VC3, I have this code :

- (void) editAd{
    AskPasswordViewController *modalViewController = [[AskPasswordViewController alloc] initWithNibName:@"AskPasswordView" bundle:nil];

    NSLog(@"modalparent class=%@", [[modalViewController parentViewController] class]);

    [self presentModalViewController:modalViewController animated:YES];
    [modalViewController release];
}

I tried this in MVC1:

- (void) sendRequest {
    NSLog(@"classe : %@",[[self parentViewController] class] );
}

but it returns my TabBarViewController...

7 Answers

If presentingViewController gives you unexpected results and you don't want to fiddle with it, just do:

class MyViewController: UIViewController {

    private weak var presenter: UIViewController?

    init(presenter: UIViewController) {
        self.presenter = presenter
        super.init(nibName: nil, bundle: nil)
        ...
    }
    
    ...
}
Related