在我正在开发的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
功能正常运行,这不是崩溃的来源。
答案 0 :(得分:1)
使用UIPageController
转换样式scroll
实现无崩溃的主要规则:
1)在调用dataSource
方法
setViewControllers
2)使用setViewControllers
方法而不使用动画(animated: false
)
3)为单页模式将dataSource
设置为nil
4)不允许2页模式的循环
找到稳定子类的完整Swift
实现
可以在GitHub project找到初始代码和用法示例。