简体   繁体   English

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

[英]Swipe between multiple view controllers using UIPageViewController

My UIPageViewController not working at all. 我的UIPageViewController根本不工作。 What I'm trying to do is switch 2 view controllers inside UIPageViewController. 我要做的是在UIPageViewController中切换2个视图控制器。 I already followed guideline here but fail. 我已经遵循指南,但失败了。 Using UIPageViewController with swift and multiple view controllers . 使用带有swift和多个视图控制器的UIPageViewController First view controller appeared successful but when I tried swipe to second view controller it throw this error message. 第一个视图控制器似乎成功,但当我尝试滑动到第二个视图控制器时,它会抛出此错误消息。 I already set all identifiers correctly. 我已经正确设置了所有标识符。

fatal error: unexpectedly found nil while unwrapping an Optional value

It's cause from here 这是因为这里的原因

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 Main.storyboard

在此输入图像描述

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

The crash occurs while trying to access the restorationIdentifier of your view controller. 尝试访问视图控制器的restorationIdentifier时发生崩溃。 You used ! 你用过! to unwrap it but it's nil ; 解开它,但它没有; as a first solution, set the restorationIdentifier in the storyboard. 作为第一个解决方案,在storyboard中设置restorationIdentifier

在此输入图像描述

In general, use ! 一般来说,使用! to unwrap a value only if you're sure it's not nil (by adding a if statement just before). 只有当你确定它不是nil时(通过在之前添加if语句)才能展开值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM