添加页面时UIPageViewController崩溃

时间:2017-11-27 15:09:48

标签: ios swift uipageviewcontroller

在我正在开发的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功能正常运行,这不是崩溃的来源。

1 个答案:

答案 0 :(得分:1)

使用UIPageController转换样式scroll实现无崩溃的主要规则:

1)在调用dataSource方法

之前设置setViewControllers

2)使用setViewControllers方法而不使用动画(animated: false

3)为单页模式将dataSource设置为nil

4)不允许2页模式的循环

可以在my detailed answer

找到稳定子类的完整Swift实现

可以在GitHub project找到初始代码和用法示例。