ios - ios试图显示视图控制器警告

  显示原文与译文双语对照的内容

我有下面的ViewControllers,在完成某些事情时呈现下一个 ViewController:

Nr1: 我的GameViewController检查游戏已经完成并调用 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];



 }];


 });


}


}



然后出现 level2ViewController:


- (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];



});



}



调用下一个 ViewController,等等。

现在我收到了以下警告:

警告:试图在视图不在窗口层次结构中的level2ViewController上显示 GameViewController !

时间: 原作者:

不要从 viewDidLoad 显示视图控制器,而是从 viewDidAppear: 调用。 同样使用 dispatch_after 作为( 假设你正在使用它来保证视图在屏幕上,而不是用于游戏目的) 是一个非常糟糕的实践。
当正在加载的视图控制器显示为( 当 viewDidAppear: 被称为) 时,你可以提供另一个视图控制器:


- (void)viewDidAppear:(BOOL)animated {


 [super viewDidAppear:animated];


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


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


}



原作者:
...