【问题标题】:Exception when presenting modal view controller: NSInternalInconsistencyException呈现模态视图控制器时出现异常:NSInternalInconsistencyException
【发布时间】:2012-12-11 16:31:23
【问题描述】:

当我尝试呈现模态视图控制器时,出现异常:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'Attempting to begin a modal transition from <UINavigationController: 0x1d906060>
to <UINavigationController: 0x1da7a6d0> while a transition is already in progress. Wait
for viewDidAppear/viewDidDisappear to know the current transition has completed'

现在,我阅读了类似的问题,他们只是稍微延迟打开模态视图,例如

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(openModalController) userInfo:nil repeats:NO];

演示文稿如下所示:

- (void)openImage:(ImageModel *)imageModel{
FullscreenImageViewController_iPhone * controller = [[FullscreenImageViewController_iPhone alloc] init];
controller.imageModel = imageModel;
UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController:controller];
UIViewController * visibleController = [[AppDelegate_iPhone app] visibleViewController];
[visibleController presentViewController:navController animated:YES completion:^{

}];
}

这不可能是真正的解决方案,对吗?如何检查我的应用程序中是否正在进行某些转换,并在当前转换完成后立即打开新的模态视图?

【问题讨论】:

  • 如果你能显示你的转换代码会很有帮助。
  • 我已经看到当您尝试从 viewDidLoad 方法呈现模式时发生此错误。那是你在做什么?如果是这样,您可以将演示文稿代码移动到 viewDidAppear。
  • 不,它在“空闲运行时”期间触发。用户点击某物,然后全屏打开某物。
  • 您要么尝试呈现一个模态视图控制器,而另一个正在被关闭(动画重叠),或者您可能以某种方式几乎同时呈现新的视图控制器两次。
  • 谢谢 rmaddy,我想这已经很清楚了。我正在寻找像队列这样的东西,它一个接一个地显示转换,或者像 UIView 的静态方法这样的东西,它可以告诉我转换是否已经在进行。有人听说过这样的事情吗?

标签: objective-c ios ios5 ios6 ios5.1


【解决方案1】:

这对我有用。如果您使用 UIViewController 的 isBeingPresented 和 isBeingDismissed 方法来测试是否正在进行展示或关闭,请稍等片刻再试一次:

- (void) presentVC {    
    if (presentingVC.isBeingPresented || presentingVC.isBeingDismissed) {
         double delayInSeconds = 0.3;
         dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
         dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
               [self presentVC];
         });
    }
    else {
        [presentingVC presentViewController:presentedVC animated:NO completion:nil];
    }
}

【讨论】:

  • 您可能从我的代码片段中可以看出,问题是有 2 个 ViewController。因此,有一个 VC 目前正在驳回某些事情,同时还有我想要打开的 VC。因此,使用您的方法,您必须拥有两个 VC 的实例,而我的大部分代码中都没有。
  • 好的。您可以将 VC 保留在一个实例变量中,直到它被呈现(即直到调用完成块)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-01
相关资源
最近更新 更多