Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access modal view controller parent

Tags:

ios

iphone

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

like image 739
Nielsou Hacken-Bergen Avatar asked Jul 18 '11 15:07

Nielsou Hacken-Bergen


3 Answers

You can access parent by calling:

self.presentingViewController

As per apple documentation:

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

like image 153
Vibhor Goyal Avatar answered Oct 14 '22 12:10

Vibhor Goyal


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

like image 25
justin Avatar answered Oct 14 '22 13:10

justin


You can always go further back just by calling parentViewController like so:

self.parentViewController.parentViewController .... and so on until you reach the right one.

like image 7
Cyprian Avatar answered Oct 14 '22 13:10

Cyprian