[英]Attempt to present View Controller Warning
I have the following ViewControllers that present the next ViewController when something is finished: 我有以下ViewControllers,它们在完成某些操作时显示下一个ViewController:
Nr1: My GameViewController checks that the game has finished and call CheckGameFinished: 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];
}];
});
}
}
Then level2ViewController appears: 然后出现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];
});
}
and called the next ViewController, and so on. 并调用下一个ViewController,依此类推。
Now I get overtime the following Warnings: 现在,我收到以下警告:
Warning: Attempt to present GameViewController on level2ViewController whose view is not in the window hierarchy!
警告:尝试在视图不在窗口层次结构中的level2ViewController上显示GameViewController!
Don't present a view controller from viewDidLoad
, instead call from viewDidAppear:
. 不要从
viewDidLoad
显示视图控制器,而是从viewDidAppear:
调用。 Also using dispatch_after
like that (assuming you are using it to hopefully make sure the view is on screen and not for gaming purposes) is a very bad practice. 像这样使用
dispatch_after
(假设您正在使用它来确保视图在屏幕上而不是出于游戏目的)是非常不好的做法。
When the view controller that is being loaded has done being presented (that happens when viewDidAppear:
is called) you can present a different one: 当正在加载的视图控制器已经完成呈现时(在调用
viewDidAppear:
时发生),您可以呈现一个不同的视图控制器:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
GameViewController *gvc = [self.storyboard instantiateViewControllerWithIdentifier:@"gameViewController"];
[self presentViewController:gvc animated:NO completion:nil];
}
我得到这个当我segue
类型是modal
,改变为push
固定对我来说
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.