可以转到UIPageViewController吗?

时间:2018-04-09 03:54:13

标签: ios swift uipageviewcontroller

我试图在用户登录后向我的应用程序添加教程部分。当应用程序加载时,他们将首先进入登录屏幕并输入其凭据。验证完成后,我想要转到UIPageViewController并向他们展示有关如何使用该应用程序的两页说明。但是,我这样做的所有尝试都导致黑屏。这是我目前的尝试:

import UIKit

class TutorialPageViewController: UIPageViewController {

    var pages: [UIViewController] = []

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.dataSource = self
        self.delegate   = self
        print("LOADING")
        let firstView = storyboard?.instantiateViewController(withIdentifier: "TutorialPage1") as! TutorialPage1ViewController
        let secondView = storyboard?.instantiateViewController(withIdentifier: "TutorialPage2") as! TutorialPage2ViewController
        pages = [firstView, secondView]
        let startingViewController = self.pages[0]
        let viewControllers: [UIViewController] = [startingViewController]
        self.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: {(done: Bool) in})
    }

    func viewControllerAtIndex(index:NSInteger) -> UIViewController {
        return pages[index]
    }
}

extension TutorialPageViewController: UIPageViewControllerDataSource
{
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

    guard let viewControllerIndex = pages.index(of: viewController) else { return nil }

        let previousIndex = viewControllerIndex - 1

        guard previousIndex >= 0          else { return pages.last }

        guard pages.count > previousIndex else { return nil        }

        return pages[previousIndex]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
    {
        guard let viewControllerIndex = pages.index(of: viewController) else { return nil }

        let nextIndex = viewControllerIndex + 1

        guard nextIndex < pages.count else { return pages.first }

        guard pages.count > nextIndex else { return nil         }

        return pages[nextIndex]
    }
}

extension TutorialPageViewController: UIPageViewControllerDelegate { }

我的viewDidLoad方法中的print语句没有被调用,所以我不明白我做错了什么。我已经从登录视图控制器创建了一个推送到TutorialPageViewController的segue,但没有任何东西加载。

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

尝试检查以下GitHub Repo上的示例项目:

链接 - https://github.com/RockinGarg/Stack_PageVC.git

包含两种不同的访问方式

1. Making use of Container View and PageVC as SubView 

2. Using Segue