我有以下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 其视图不在窗口层次结构中!
答案 0 :(得分:1)
不要来自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];
}
答案 1 :(得分:0)
当Builder Pattern
类型为segue
时,我收到此消息,更改为modal
为我修复了