gpt4 book ai didi

ios - 尝试呈现 View Controller 警告

转载 作者:行者123 更新时间:2023-11-29 02:03:30 25 4
gpt4 key购买 nike

我有以下 ViewController,当某事完成时显示下一个 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,依此类推。

现在我超时收到以下警告:

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

最佳答案

不要从 viewDidLoad 呈现 View Controller ,而是从 viewDidAppear: 调用。同样使用 dispatch_after (假设您使用它是为了确保 View 显示在屏幕上而不是用于游戏目的)是一种非常糟糕的做法。
当正在加载的 View Controller 完成呈现时(调用 viewDidAppear: 时发生),您可以呈现不同的 View Controller :

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
GameViewController *gvc = [self.storyboard instantiateViewControllerWithIdentifier:@"gameViewController"];
[self presentViewController:gvc animated:NO completion:nil];
}

关于ios - 尝试呈现 View Controller 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30076714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com