0

I have the following ViewControllers that present the next ViewController when something is finished:

Nr1: My GameViewController checks that the game has finished and call CheckGameFinished:

-(void) checkGameFinished {
if ([self.gameModel isGameOver]) {

    double delayTimeInSeconds = 3.5;
    dispatch_time_t popTimeDelay = dispatch_time(DISPATCH_TIME_NOW, delayTimeInSeconds * NSEC_PER_SEC);
    dispatch_after(popTimeDelay, dispatch_get_main_queue(), ^(void){

    [progressBarTimer invalidate];

    level2ViewController *govc = [self.storyboard instantiateViewControllerWithIdentifier:@"level2ViewController"];

    [self.finishAudio play];

    [self presentViewController:govc animated:NO completion:^(){
        [self.gameModel clearGameData];

    }];
          });
}
}

Then level2ViewController appears:

- (void)viewDidLoad {
[super viewDidLoad];

double delayTimeInSeconds = 2;
dispatch_time_t popTimeDelay = dispatch_time(DISPATCH_TIME_NOW, delayTimeInSeconds * NSEC_PER_SEC);
dispatch_after(popTimeDelay, dispatch_get_main_queue(), ^(void){


    GameViewController *gvc = [self.storyboard instantiateViewControllerWithIdentifier:@"gameViewController"];

    [self presentViewController:gvc animated:NO completion:nil];


});

}

and called the next ViewController, and so on.

Now I get overtime the following Warnings:

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

Matteo Alessani
  • 10,264
  • 4
  • 40
  • 57
iOSBeginner
  • 103
  • 8
  • possible duplicate of [whose view is not in the window hierarchy](http://stackoverflow.com/questions/11862883/whose-view-is-not-in-the-window-hierarchy) – Vivek Molkar May 06 '15 at 12:32
  • Are you creating new view controllers forever? `GameViewController` instantiates a `level2ViewController` which instantiates a `GameViewController` which.... – Phillip Mills May 06 '15 at 12:35
  • the view controller is in my storyboard, and i want just to call them without segues – iOSBeginner May 06 '15 at 12:43
  • The view controller is in memory; its **description** is in the storyboard. Every time you call "instantiate..." you create a new one. – Phillip Mills May 06 '15 at 14:47
  • Oh ok thanks for this Information. So how can i "repair" My Problem? Dealloc After calling? – iOSBeginner May 06 '15 at 15:07

2 Answers2

1

Don't present a view controller from viewDidLoad, instead call from viewDidAppear:. Also using dispatch_after like that (assuming you are using it to hopefully make sure the view is on screen and not for gaming purposes) is a very bad practice.
When the view controller that is being loaded has done being presented (that happens when viewDidAppear: is called) you can present a different one:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    GameViewController *gvc = [self.storyboard instantiateViewControllerWithIdentifier:@"gameViewController"];
    [self presentViewController:gvc animated:NO completion:nil];
}
Aviel Gross
  • 9,770
  • 3
  • 52
  • 62
  • but in my game i can't call it from viewdidappear. i want to call it when the method CheckGameFinished was called... For the other view controller it is working as intended, thanks – iOSBeginner May 06 '15 at 12:42
  • I was referring to your 2nd piece of code which tries to present a view controller from `viewDidLoad` – Aviel Gross May 06 '15 at 12:46
  • Glad it worked, if the answer solved your problem please consider marking the green check for the answer, thanks (: – Aviel Gross May 06 '15 at 12:57
0

I was getting this when my segue type was modal, changing to push fixed it for me

wantrapreneur
  • 410
  • 4
  • 13