使用 UIPageViewController 在多个视图控制器之间滑动 [英] Swipe between multiple view controllers using UIPageViewController

查看:30
本文介绍了使用 UIPageViewController 在多个视图控制器之间滑动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 UIPageViewController 根本不工作.我想要做的是在 UIPageViewController 中切换 2 个视图控制器.我已经在这里遵循指南但失败了.

解决方案

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

<块引用>

一般来说,使用 ! 来解开一个值,如果你确定它不是 nil(通过在前面添加一个 if 语句).

My UIPageViewController not working at all. What I'm trying to do is switch 2 view controllers inside UIPageViewController. I already followed guideline here but fail. Using UIPageViewController with swift and multiple view controllers. 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

Source code: https://github.com/datomnurdin/RevivalxSwiftPageViewController

解决方案

The crash occurs while trying to access the restorationIdentifier of your view controller. You used ! to unwrap it but it's nil ; as a first solution, set the restorationIdentifier in the storyboard.

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

这篇关于使用 UIPageViewController 在多个视图控制器之间滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆