多次调用setViewControllers(direction:animated:completion :)时崩溃

时间:2020-09-24 03:43:26

标签: ios uipageviewcontroller

我有基于索引的自定义UIPageViewController,在其中尝试实现显示下一页或上一页的move方法。 SO上的大多数答案都告诉我们,以编程方式设置当前页面的唯一方法是调用setViewControllers(direction:animated:completion:)方法,但是当我从按钮调用此方法并单击很多时,很快会抛出异常:

Exception   NSException *   "Unexpected view controller: <MyPageViewController: 0x106175ef0>"   0x00000002800fa460

name    __NSCFConstantString *  "NSInternalInconsistencyException"  0x0000000200e3dac0
reason  __NSCFString *  "Unexpected view controller: <MyPageViewController: 0x106175ef0>"   0x0000000282492680

这是我的move(direction:)方法:

func move(_ direction: UIPageViewController.NavigationDirection) {
    var destination: MyPageViewController!
    switch direction {
        case .forward: destination = self.nextPage?.viewController
        case .reverse: destination = self.prevPage?.viewController
        @unknown default: fatalError("\(type(of: self)): Unknow direction = \(direction)")
    }

    self.setViewControllers([destination], direction: direction, animated: true, completion: nil)
}

在我的自定义UIPagerViewController中,我拥有3个页面,并在UIPageViewControllerDataSource中在它们之间循环移动。

为什么会发生此错误?现在,我禁用下一个按钮,直到页面更改结束(通过completion arg),但由于它限制了页面更改的速度,因此我认为这不是一个好的解决方案。

以下是一些错误日志:

2020-09-24 11:24:06.619878+0800 MyApp[976:249262] *** Assertion failure in -[MyUIPageViewController queuingScrollView:willManuallyScroll:toRevealView:concealView:animated:], /Library/Caches/com.apple.xbs/Sources/UIKitCore/UIKit-3920.31.102/UIPageViewController.m:1959
2020-09-24 11:24:06.620689+0800 MyApp[976:249262] 
2020-09-24 11:24:06.620813+0800 MyApp[976:249262] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unexpected view controller: <MyApp.MyPageViewController: 0x106175ef0>'
*** First throw call stack:
(0x1bf749654 0x1bf46bbcc 0x1bf64c6ec 0x1bfa9216c 0x1c314c940 0x1c3156d94 0x1c31581fc 0x1c3157d50 0x1c3153f78 0x1c3cb17fc 0x1c624e494 0x1c62545ec 0x1c625f128 0x1c61a7b44 0x1c61d14e4 0x1c610e120 0x1c0698650 0x1bf69eb34 0x1bf6c8174 0x1bf6c7880 0x1bf6c2634 0x1bf6c1ba8 0x1c9831344 0x1c37fd3e4 0x1049dcea0 0x1bf5498f0)
libc++abi.dylib: terminating with uncaught exception of type NSException

0 个答案:

没有答案
相关问题