UIPageViewController有bug

时间:2018-02-15 13:43:34

标签: ios swift uipageviewcontroller

当DataSource方法ViewControllerAfterViewControllerBefore返回nil时,我的UIPageViewController崩溃(这应该只表示不再显示页面)。它与消息崩溃:

  

'提供的视图控制器数(0)与数字不匹配   要求转换'

需要(1)

谢谢!请帮忙!

我的初始化代码:

self.pageController = UIPageViewController(transitionStyle: .pageCurl, navigationOrientation: .horizontal, options: nil)
                self.pageController?.dataSource = self
           //     self.pageController?.delegate = self
                self.pageController?.setViewControllers([self.controller.pages.first!], direction: .forward, animated: true, completion: nil)
                self.updateControls()
                self.addChildViewController(self.pageController!)
                self.view.addSubview((self.pageController?.view)!)
                self.pageController?.didMove(toParentViewController: self)

在我的DataSource代码下面:

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

    previousIndex = self.controller.pages[self.currentPage].pageIndex!

    if previousIndex <= 0  {
        print("entrou no nil do before")
        return nil
    }

    self.previousIndex -= 1
    self.currentPage = self.previousIndex
    return self.controller.pages[previousIndex]
}

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

    nextIndex = self.controller.pages[self.currentPage].pageIndex!

    nextIndex += 1
    if (nextIndex == self.controller.pages.count) {
        print("entrou no nil do after")
        return nil
    }
    self.currentPage = nextIndex
    return self.controller.pages[nextIndex]
}

1 个答案:

答案 0 :(得分:0)

由于其行为,这两种方法都将返回nil。你需要自己处理它。 这种灵活性用于将页面视图控制器中的页面放在循环中或以页面数结束。

您需要使用此类代码:

// MARK: - PageViewControllerDataSource

// viewcontroller(pages)标识符数组。

var pageTitles = [String]

[ 0.12 -0.12]