不调用UIPageViewController的数据源方法

时间:2016-02-10 18:26:28

标签: ios swift uitableview uipageviewcontroller

我的iOS应用中有以下代码:

class BannerTableViewCell: UITableViewCell, UIPageViewControllerDataSource {
private var pageViewController: UIPageViewController!
private var pages: [UIViewController] = []

override func awakeFromNib() {
    super.awakeFromNib()
    self.backgroundColor = UIColor.clearColor()
    self.backgroundView?.backgroundColor = UIColor.clearColor()
    self.contentView.backgroundColor = UIColor.clearColor()
    self.selectionStyle = UITableViewCellSelectionStyle.None

    pageViewController = UIPageViewController();

    self.pageViewController.dataSource = self
    pageViewController.view.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: 130)
    self.addSubview(pageViewController.view)

    //Example data
    let v1 = UIViewController()
    v1.view.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: 130);
    v1.view.backgroundColor = UIColor.blueColor()

    let v2 = UIViewController()
    v2.view.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: 130);
    v2.view.backgroundColor = UIColor.greenColor()

    pages.append(v1)
    pages.append(v2)
}



func pageViewController(pageViewController: UIPageViewController,
    viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = pages.indexOf(viewController) else {
            return nil
        }

        let previousIndex = viewControllerIndex - 1

        guard previousIndex >= 0 else {
            return nil
        }

        guard pages.count > previousIndex else {
            return nil
        }

        return pages[previousIndex]
}

func pageViewController(pageViewController: UIPageViewController,
    viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = pages.indexOf(viewController) else {
            return nil
        }

        let nextIndex = viewControllerIndex + 1
        let orderedViewControllersCount = pages.count

        guard orderedViewControllersCount != nextIndex else {
            return nil
        }

        guard orderedViewControllersCount > nextIndex else {
            return nil
        }

        return pages[nextIndex]
}

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

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

我在表视图中实例化此单元格,但此单元格中的页面视图控制器始终为空,并且未调用pageViewController的数据源方法。你知道他们为什么不被召唤吗?

3 个答案:

答案 0 :(得分:1)

您应该使用记录的初始化程序来实例化UIPageViewController:

public init(transitionStyle style: UIPageViewControllerTransitionStyle, navigationOrientation: UIPageViewControllerNavigationOrientation, options: [String : AnyObject]?)

您在awakeFromNib末尾创建的ViewControllers也可以立即放入pageViewController。

pageViewController.setViewControllers([v1, v2], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)

答案 1 :(得分:0)

如果同时实现了这两种方法,过渡样式为“ UIPageViewControllerTransitionStyleScroll”,导航方向为“ UIPageViewControllerNavigationOrientationHorizo​​ntal”,则页面指示符将可见。

两个方法都被调用以响应“ setViewControllers:...”调用,但是在手势驱动导航的情况下,表示索引会自动更新。

答案 2 :(得分:0)

这也可能是因为页面视图控制器不是您的根视图控制器。

您可以在segue中添加以下代码:

let x = (UIApplication.shared.delegate as! AppDelegate).window!
x.rootViewController = yourViewController()