警告窗口层次结构

时间:2012-10-03 08:54:03

标签: iphone objective-c ios xcode

我的调试器中有这样的警告,这是什么意思?

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

我还将MovieViewController作为Storyboard中的初始视图控制器,我也使用ARC。

注意:当我在[super viewDidLoad];的最低部分中间添加两个viewDidLoad 1时,这不会出现,这是错误的。

以下是我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];   
}

非常感谢帮助:)

2 个答案:

答案 0 :(得分:2)

当您的ViewController的viewDidLoad加载到内存中时,将调用view方法。但到那时,view尚未显示在屏幕上(因此它尚未添加为任何其他subviewview的{​​{1}},也就是说它不在视图层次结构)。

您应该等待屏幕上的视图显示您的window。 请改为使用MovieViewController方法和viewDidAppear:移动代码,您应该没问题。

答案 1 :(得分:0)

viewDidLayoutSubviews可用于此目的。它早于viewDidAppear。