【发布时间】:2017-11-27 15:09:47
【问题描述】:
在我正在开发的 iOS 11 应用程序中(使用 Swift 4),我需要将页面动态添加到 UIPageViewController。但是,在某些情况下,我会收到以下崩溃错误 (NSInternalInconsistencyException):
2017-11-27 14:55:54.787260+0000 MyApp[380:79434] *** Assertion failure in -[UIPageViewController _flushViewController:animated:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3698.21.8/UIPageViewController.m:2124
2017-11-27 14:55:54.791022+0000 MyApp[380:79434] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Don't know about flushed view <UIView: 0x12dd48ac0; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x1c003c280>>'
我对 iOS 开发非常陌生,所以如果答案很明显,我很抱歉,但我很难找到崩溃的根本原因。记录的消息也不是很有帮助...
这是我用来添加页面的代码:
//Inflate the view controller
let viewController = newViewController(param: someParam )
//Add it to the view controllers pool
viewControllersOrdered.insert(viewController, at: getInsertionIndex(param: someOtherParam) )
//Add it to the pageViewController
pageViewController.setViewControllers([viewController], direction: .forward, animated: false, completion: nil)
//Force cache to be cleared
pageViewController.dataSource = nil;
pageViewController.dataSource = self;
getInsertionIndex 函数工作正常,这不是崩溃的根源。
【问题讨论】:
-
你在做什么很奇怪。手头有一个带有 UIPageViewController 的视图控制器池是不正常的。整个想法是它只有 一个 视图控制器。否则,你就是在浪费内存。您必须“清除缓存”的事实表明这是不正常的,并且不会起作用;你不能干涉 UIPageVC 的缓存机制。您应该只实现委托方法并让 UIPageViewController 进行缓存并管理子视图控制器。
-
@matt 您好,感谢您的回复。我正在清除缓存,因为当我删除视图时,viewController 仍然会让用户滚动到它们,因为 getBefore/After 方法已经被调用。 pageViewController 界面似乎确实在与页面的动态添加/删除作斗争。我错过了什么吗?
-
你什么都没有错过!你是绝对正确的。问题是您选择了一个视图控制器架构,然后您必须与之抗争。那是难闻的气味。请参阅下面的答案。
-
也许这篇文章中的答案:assertion-failure-in-uipageviewcontroller) 可以帮到你。如果没有,请提供更多代码...而且“清除缓存”也不正常。
标签: ios swift uipageviewcontroller