Bazinga
Bazinga

Reputation: 2466

Warning about window hierarchy

I having a warning like this in my debugger, What does this mean?

Warning: Attempt to present <RootViewViewController: 0x134ce9c0> on <MovieViewController: 0xa967290> whose view is not in the window hierarchy!

I also make the MovieViewController as the initial view controller in Storyboard, I am also using ARC.

NOTE: This doesnt appear when I add two [super viewDidLoad]; 1 in the middle in lowest part of the viewDidLoad,which is wrong.

Here is my implementation of the MovieViewController:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"gameopening" ofType:@"m4v"];
    self.moviePlayer = [AVPlayer playerWithURL:[NSURL fileURLWithPath:moviePath]];
    [self.moviePlayer play];

    // Create and configure AVPlayerLayer
    AVPlayerLayer *moviePlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.moviePlayer];
    moviePlayerLayer.bounds = CGRectMake(0, 0, 1024, 768);
    moviePlayerLayer.position = CGPointMake(515,385);
    moviePlayerLayer.borderColor = [UIColor clearColor].CGColor;
    moviePlayerLayer.borderWidth = 3.0;
    moviePlayerLayer.shadowOffset = CGSizeMake(0, 3);
    moviePlayerLayer.shadowOpacity = 0.80;

    // Add perspective transform

    [self.view.layer addSublayer:moviePlayerLayer];    

    [self performSelector:@selector(loadingView) withObject:nil afterDelay:33.0];  
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    tapGesture.numberOfTapsRequired = 1;
    [self.view addGestureRecognizer:tapGesture];

}

- (void)handleTapGesture:(UITapGestureRecognizer *)sender {

    if (sender.state == UIGestureRecognizerStateEnded) {
        UIImageView *loadingView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)]; 
        loadingView.image = [UIImage imageNamed:@"Load.png"];
        [self.view addSubview:loadingView];
        [self performSelector:@selector(mainV) withObject:nil afterDelay:2.0];  
    }

}
-(void)loadingView{

        UIImageView *loadingView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];  
        loadingView.image = [UIImage imageNamed:@"Load.png"];
        [self.view addSubview:loadingView];
        [self performSelector:@selector(mainV) withObject:nil afterDelay:2.0];  

}
-(void)mainV {

        moviePlayer = nil;
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"mainViewController"];
        vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:vc animated:YES completion:NULL];   
}

Help would be much appreciated :)

Upvotes: 0

Views: 4361

Answers (2)

Jonny
Jonny

Reputation: 16298

viewDidLayoutSubviews can be used for this purpose. It is earlier than viewDidAppear.

Upvotes: 0

AliSoftware
AliSoftware

Reputation: 32681

The viewDidLoad method is called when the view of your ViewController is loaded into memory. But by that time, the view is not on screen yet (so it has not been added as a subview of any other view or window, a.k.a it is not in the view hierarchy).

You should wait for the view to be on screen to present your MovieViewController. Move your code in the viewDidAppear: method instead and viewDidLoad and you should be OK.

Upvotes: 2

Related Questions