Container中的UIPageViewController不显示其他视图控制器

时间:2017-03-10 12:00:25

标签: ios swift xcode8 viewcontroller uipageviewcontroller

好的,有人可以帮我解决我正在尝试修复的Swift代码。 我在这里读到了与我类似的其他问题,但不幸的是无济于事。

问题是我在容器中有一个UIPageViewController,但是当我在iPhone上测试它时,它不会显示我设置的两个视图(实际上有9个视图,但现在只在代码中放入2个我可以让它运作起来。)

这是我的代码:

import UIKit

class pvController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    var pages = [UIViewController]()


    override func viewDidLoad() {
        super.viewDidLoad()

        self.delegate = self
        self.dataSource = self

        let page1: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "vController1")
        let page2: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "vController2")

        pages.append(page1)
        pages.append(page2)

        setViewControllers([page1], direction: UIPageViewControllerNavigationDirection.forward, animated: false)
        // Do any additional setup after loading the view, typically from a nib.
    }

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

        let currentIndex = pages.index(of: viewController)
        let previousIndex = abs((currentIndex! - 1) % pages.count)
        return pages[previousIndex]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

        let currentIndex = pages.index(of: viewController)
        let nextIndex = abs((currentIndex! + 1) % pages.count)
        return pages[nextIndex]
    }

    func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return pages.count
    }

    func presentationIndex(for pageViewController: UIPageViewController) -> Int {
        return 0
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

我最初尝试使用SwipeGestureRecogniser进行操作,虽然我无法让它工作/搞清楚。

此外,我对此很新,所以它可能是一些简单的事情,我无法弄明白。

提前致谢

0 个答案:

没有答案