UIPageViewController - 不符合协议

UIPageViewController - Does not conform to protocol

我正在尝试实现 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 个新函数的函数声明主体中显示相同的 + 预期 {。

有人可以帮忙吗?

你需要像这样使用它,因为你正在使用 UIPageViewController 而你没有实现 UIPageViewController 的委托方法 viewControllerAfterviewControllerBefore 这就是为什么你收到此错误。

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()
    }
}