0

I have a view controller named ViewController.

  • ViewController displays a UIView that has a button on it, which allows me to advance to a second view controller - SecondViewController.
  • SecondViewController also has a button on it, which allows me to advance to a third view controller.

However, I am having trouble displaying ThirdViewController. When I tap the button in SecondViewController it throws an error:

Warning: Attempt to present ... on ... whose view is not in the window hierarchy!

I have been to a bunch of other sites and posts that address this issue, but cannot seem to find a working solution. I have implemented quite a few solutions, but none seem to work.

Here is my code:

- (void)secondaryView:(UIView *)secondaryView
{
    UIView *theView = secondaryView;

    UIViewController *viewController = [[UIViewController alloc] init];
    viewController.view.frame = [UIScreen mainScreen].bounds;

    [viewController.view addSubview:theView];
    viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:viewController animated:YES completion:nil];
}

secondaryView is a UIView that I am constructing elsewhere in the application. I add it to the viewController then present the viewController.

Is there any way to dynamically create UIViewControllers, add a UIView to each one, and add them to the window hierarchy?

4
  • This is not how you do it. Just instantiate the next controller, and present it. You don't add its view as a subview. Commented May 15, 2013 at 5:27
  • secondaryView is a separate view that I am constructing elsewhere and want to add it to the new ViewController. Sorry if that was not clear, I will make an edit to the post. Either way, I tried your solution, and unfortunately it did not fix my problem. Commented May 15, 2013 at 5:33
  • What controller is the code you posted in? Is that controller's view on screen when you tap the button? Commented May 15, 2013 at 6:26
  • Can you use navigation in your viewControllers? Commented May 15, 2013 at 6:56

1 Answer 1

1

If you can use navigation in your ViewControllers, you can try the below code

In the place where you call the firstViewController,

   -(void)callFirst
    {
    FirstViewController *first = [FirstViewController alloc] init];
    UINavigationController *navigationController = [UINavigationController alloc] initWithRootViewController:first];
    navigationController.modalPresentaionStyle = UIModalPresenationFormSheet;
    [self presentModalViewController:navigationController animated:YES];
}

And in the FirstViewController button action ,you can write

-(void)callSec
{
SecondViewController *sec = [SecondViewController alloc] init];
[self.NavigationController pushViewController:sec animated:YES];
}

And in SecondViewController you can do the same

  -(void)callThird
    {
        ThirdViewController *third = [ThirdViewController alloc] init];
        [self.NavigationController pushViewController:third animated:YES];

    }

Try this...And in these ViewControllers you can do whatever coding you want to meet the requirement like

-(void)viewDidLoad
{
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(20,20,400,400)];
newView.backGroundColor = [UIColor redColor];
[self.view addSubView:newView];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.