【问题标题】:Attempt to present View Controller Warning尝试显示视图控制器警告
【发布时间】:2015-07-16 14:07:29
【问题描述】:

我有以下 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,以此类推。

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

警告:尝试在 level2ViewController 上呈现 GameViewController 其视图不在窗口层次结构中!

【问题讨论】:

  • 您是否一直在创建新的视图控制器? GameViewController 实例化了一个 level2ViewController,它实例化了一个 GameViewController,它......
  • 视图控制器在我的情节提要中,我只想在没有转场的情况下调用它们
  • 视图控制器在内存中;它的描述在情节提要中。每次调用“实例化...”时,都会创建一个新的。
  • 哦,好的,感谢您提供此信息。那么我该如何“修复”我的问题呢? Dealloc 调用后?

标签: ios objective-c uiviewcontroller


【解决方案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];
}

【讨论】:

  • 但在我的游戏中我无法从 viewdidappear 调用它。我想在调用 CheckGameFinished 方法时调用它...对于其他视图控制器,它按预期工作,谢谢
  • 我指的是你的第二段代码,它试图从viewDidLoad展示一个视图控制器
  • 很高兴它成功了,如果答案解决了您的问题,请考虑将答案标记为绿色,谢谢 (:
【解决方案2】:

当我的 segue 类型为 modal 时,我得到了这个,更改为 push 为我修复了它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 2011-07-24
    相关资源
    最近更新 更多