UIPageViewController - 不符合协议

时间:2016-11-24 22:18:55

标签: ios swift uiview swift3 uipageviewcontroller

我正在尝试实现UIPageViewController,我收到以下错误:我的代码如下

import UIKit

class ShopViewController: UIPageViewController, UIPageViewControllerDataSource {

  override func viewDidLoad() {
    super.viewDidLoad()
    }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    }
}

这给了我一个错误:ShopViewController不符合协议UIPageViewControllerDataSource。

所以我发现另一个线程建议我添加以下函数:

func pageViewController(pageViewController: UIPageViewController,      viewControllerAfterViewController viewController: UIViewController) -> UIViewController?

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?

完成后我的代码现在是:

class ShopViewController: UIPageViewController, UIPageViewControllerDataSource {

   func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?

   func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?

   override func viewDidLoad() {
    super.viewDidLoad()

   }

   override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
   }
  }

但是我仍然得到一个错误,说明相同的+期望{在函数声明的主体中我添加了2个新函数。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

您需要像这样使用它,因为您正在使用UIPageViewController并且您没有实现viewControllerAfter的委托方法viewControllerBeforeUIPageViewController这就是您获得的原因这个错误。

class ShopViewController: UIPageViewController, UIPageViewControllerDataSource {

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        // Returns the view controller after the given view controller.
        return self
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        // Returns the view controller before the given view controller.

        return self
    }

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}