使用UIPageViewController在多个视图控制器之间切换。

4

我的UIPageViewController完全没用。我正在尝试在UIPageViewController中切换2个视图控制器。 我已经按照这里的指南进行了操作,但失败了。 使用Swift和多个视图控制器的UIPageViewController。 第一个视图控制器出现成功,但当我尝试向第二个视图控制器滑动时,它会抛出此错误消息。 我已经正确设置了所有标识符。

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)

    }

主.storyboard

输入图像说明

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


请注意,我已经将内容翻译成了中文,并且保留了HTML标签。

每日一句:「我的UIPageViewController完全不工作。」同样的問題... - krummens
1个回答

3
尝试访问视图控制器的 restorationIdentifier 时发生崩溃。您使用了 ! 进行解包,但它是 nil;作为第一种解决方案,请在故事板中设置 restorationIdentifier。 enter image description here 总的来说,仅当您确定值不为 nil(通过在之前添加 if 语句)时,使用 ! 解包一个值。

1
它对我有效。我将在您的GitHub存储库上提交一个PR,以便您可以查看结果。 - sweepy_
刚刚在你的代码库上提交了一个PR。在Xcode 6.2 (b. 6C131e)下进行了测试。 - sweepy_

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接