Dex
Dex

Reputation: 12749

Presenting Main View Controller from a Child View Controller

I have a main view controller and am presenting a child view. For one of the settings inside the child, I need to call the main view controller again. It would just make things a lot easier.

The following causes a hierarchy problem:

#define appDelegate (MyAppDelegate *)[[UIApplication sharedApplication] delegate]

[self presentViewController:[appDelegate mainViewController] animated:YES completion:^{}];

Is there any way to call the mainViewController easily while not losing the state of the child view?

EDIT

Here is how I'm presenting the child view.

ChildViewController *childViewController = [[ChildViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:childViewController];
[childViewController release];
[self presentModalViewController:navigationController animated:NO];
[navigationController release];

Upvotes: 0

Views: 286

Answers (2)

Vinnie
Vinnie

Reputation: 1740

You can't present a view controller that has already been presented or is in a navigation stack. What is the mainViewController? Did you instantiate it? Has it been added to the screen yet?

If yes to both, you either need to back into it (dismiss to it) or remove it from it's parent first and then present it.

Upvotes: 2

Simon Germain
Simon Germain

Reputation: 6844

In your child view, you should have access to your application delegate and its mainViewController property, like this:

    MyApplicationDelegate *appDelegate = (MyApplicationDelegate *)[[UIApplication sharedApplication] delegate];

Upvotes: 0

Related Questions