Swipe between multiple view controllers using UIPageViewController

六眼飞鱼酱① 提交于 2019-12-10 15:13:59

问题


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


回答1:


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).



来源:https://stackoverflow.com/questions/28734103/swipe-between-multiple-view-controllers-using-uipageviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!