ios - 异常 "Application tried to present modally an active controller"仅在 iOS 8 中崩溃

标签 ios uikit ios8

UIPopoverController *popCtrl = [[UIPopoverController alloc] initWithContentViewController:self.rootViewController.navigationController];
popCtrl.delegate = self;
[popCtrl presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

此代码位于按钮操作中,其中按钮是“发送者”。

带有 presentPopoverFromBarButtonItem 的行导致抛出异常,原因是:Application tried to present modally an active controller DetailViewController: 0x15a54c00。在这种情况下,DetailViewController 是“self”,它只是 popCtrl 的委托(delegate),所以我看不出它是如何尝试以模态方式呈现的。它应该呈现 rootViewController.navigationController。

正如您可能已经从名称中猜到的那样,rootViewController 和 detailViewController 位于 SplitViewController 中,但在尝试使用弹出窗口呈现 rootViewController 之前,它已从 SplitViewController 中删除。

这只会在使用 iOS 8 SDK 构建的 iOS 8 上发生。它也不是 100% 可重现的。大多数情况下会发生此异常,但有时在我重新启动应用程序后它根本不会发生,直到我重新运行应用程序,然后它再次开始发生。 (我把它放在 try/catch 中,所以我知道每次运行它可能发生不止一次。)

我几乎可以肯定这是 SDK 中的另一个 iOS 8 错误,但是有人想出解决方法吗?

最佳答案

我在更新一些应用程序时遇到了同样的问题,该应用程序最初是在 iOS 5.0 时代开发的。在弹出窗口中使用它之前从 UISplitViewController 中删除 Controller 没有用,也没有帮助切换到更新的 UIPopoverPresentationController。

但是,我能够从左侧滑入我的(主) Controller 。或多或少,我无意中发现了这个“功能”,所以我查找了它的来源,并在 Apple 的 iOS SDK 5.1 发行说明中找到了这个:

In 5.1 the UISplitViewController class adopts the sliding presentation style when presenting the left view (previously only seen in Mail). This style is used when presentation is initiated either by the existing bar button item provided by the delegate methods or by a swipe gesture within the right view. No additional API adoption is required to obtain this behavior, and all existing API, including that of the UIPopoverController instance provided by the delegate, will continue to work as before. If the gesture cannot be supported in your app, set the presentsWithGesture property of your split view controller to NO to disable the gesture. However, disabling the gesture is discouraged because its use preserves a consistent user experience across all applications.

(来源:iOS 5.1 Release Notes,需要 Apple 开发者登录)

我没有测试如果将提到的属性设置为 NO 并且它释放 Controller 会发生什么,但我不会对此抱太大希望。

因此,即使在从 UISplitViewController 中手动删除它之后,我的 View Controller 在那个隐藏的可滑动 Pane 上仍然处于事件状态,这似乎发生在 SDK 内部。我知道这在 iOS 7.x 之前仍然可以正常工作,但实际上我现在认为这是可以容忍的错误,在 iOS 8.0 中关闭。

我最终完全放弃了弹出窗口并使用了 iOS 5.1 及更高版本的默认 UISplitViewController 行为。对于一些额外的调整,您可以更改 UISplitViewController.preferredDisplayMode 以满足您的需求,这为我节省了很多时间来升级从未听说过自动布局的旧代码。

关于ios - 异常 "Application tried to present modally an active controller"仅在 iOS 8 中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26268342/

相关文章:

iOS 分享扩展如何支持 *.wav 文件

objective-c - Swift 中的 MFMailComposeViewController

ios - 如何获取联系方式(名字、姓氏、联系人 ID、电话号码)

ios - 如何在一个 ARAnchor 上固定多个 SKNode?

ios - 界面生成器中的 "Builds for"选项

ios - CAKeyframeAnimation - 沿路径移动并使用缓动

iphone - 在 OpenGL ES View 和 UIKit View Controller 之间切换

objective-c - UIGlass 按钮的工作

ios - 怎么打重法?

iphone - 从 NSMutableData 即时创建 PDF