I have index-based custom UIPageViewController in which I try to implement a move method that show next or previous page. Most answers on SO told that only way to set current page programmatically is calling setViewControllers(direction:animated:completion:) method but when i call this method from button and click it a lot and quickly Exception will throw:
Exception NSException * "Unexpected view controller: <MyPageViewController: 0x106175ef0>" 0x00000002800fa460
name __NSCFConstantString * "NSInternalInconsistencyException" 0x0000000200e3dac0
reason __NSCFString * "Unexpected view controller: <MyPageViewController: 0x106175ef0>" 0x0000000282492680
Here is my move(direction:) method:
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)
}
In my custom UIPagerViewController I hold 3 pages and loop moving between them in the UIPageViewControllerDataSource.
Why is this error happening? For now, I disable the next button until the page change ends (via completion arg) but I don't think this is a good solution due to the fact that it limits the speed of page changes.
Here is some error logs:
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