使用 UIPageViewController 在多个视图控制器之间滑动

Swipe between multiple view controllers using UIPageViewController

我的 UIPageViewController 根本不工作。我想要做的是在 UIPageViewController 中切换 2 个视图控制器。我已经按照此处的指南进行操作但失败了。 Using UIPageViewController with swift and multiple view controllers。第一个视图控制器看起来很成功,但是当我尝试滑动到第二个视图控制器时,它会抛出此错误消息。我已经正确设置了所有标识符。

fatal error: unexpectedly found nil while unwrapping an Optional value

原因来自这里

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {

        let identifier = viewController.restorationIdentifier
        let index = self.identifiers.indexOfObject(identifier!) // error message here

        //if the index is 0, return nil since we dont want a view controller before the first one
        if index == 0 {

            return nil
        }

        //decrement the index to get the viewController before the current one
        self.index = self.index - 1
        return self.viewControllerAtIndex(self.index)

    }

Main.storyboard

源代码:https://github.com/datomnurdin/RevivalxSwiftPageViewController

尝试访问视图控制器的 restorationIdentifier 时发生崩溃。您使用了 ! 来解包,但它是 nil ;作为第一个解决方案,在故事板中设置 restorationIdentifier

In general, use ! to unwrap a value only if you're sure it's not nil (by adding a if statement just before).