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

我的UIPageViewController根本不工作。 我想要做的是在UIPageViewController内切换2视图控制器。 我已经在这里遵循指导,但失败。 使用UIPageViewController和swift和多视图控制器 。 第一个视图控制器出现成功,但是当我试图刷到第二个视图控制器它抛出这个错误信息。 我已经正确设置了所有标识符。

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

请指教。 谢谢。

尝试访问视图控制器的restoreIdentifier时发生崩溃。 你用过! 打开它,但它是零; 作为第一个解决scheme,在故事板中设置restorationIdentifier

在这里输入图像说明

一般来说,使用! 只有在确定它不是零(通过在之前添加一个if语句)才能打开一个值。