首页 > 解决方案 > 多次调用 setViewControllers(direction:animated:completion:) 时崩溃

问题描述

我有基于索引的自定义 UIPageViewController,我尝试在其中实现显示下一页或上一页的移动方法。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

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

以下是一些错误日志:

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

标签: iosuipageviewcontroller

解决方案


推荐阅读