未调用 UIPageViewController 数据源

UIPageViewControllerDataSource is not called

未从 ViewDidLoad 调用 UIPageViewControllerDataSource,因此仅显示第一页,滑动不执行任何操作。

我已经学习了这个教程:

https://spin.atomicobject.com/2015/12/23/swift-uipageviewcontroller-tutorial/

类似问题的答案无效。我已经查看了 5 个类似的问题,但其中 none 对我有用。


class SetupViewController: UIPageViewController {

      private(set) lazy var pages : [UIViewController] = {
         return [self.newPage(1), self.newPage(2), self.newPage(3), self.newPage(4)]
      }()

      private func newPage(_ page: Int) -> UIViewController {
          return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Page\(page)")
      }

      override func viewDidLoad() {
            super.viewDidLoad()  

            dataSource = self

            if let firstPage = pages.first {
                print(#function, "if let firstPage", firstPage, pages)  //viewDidLoad() if let firstPage page1name [page1, page2, page3, page4]
                setViewControllers([firstPage], direction: .forward, animated: true, completion: nil)

          }
     }
}

extension SetupViewController: UIPageViewControllerDataSource {

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        print(#function)           //It doesn't print
        guard let pageIndex = pages.firstIndex(of: viewController) else { return nil }
        let nextIndex = pageIndex + 1
        let pageCount = pages.count
        guard nextIndex != pageCount else { return nil }
        guard pageCount > nextIndex else { return nil }
        return pages[nextIndex]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        print(#function)            //It doesn't print
        guard let pageIndex = pages.firstIndex(of: viewController) else { return nil }
        let previousIndex = pageIndex - 1
        guard previousIndex >= 0 else { return nil }
        guard pages.count > previousIndex else { return nil }
        return pages[previousIndex]
    }

}

Storyboard IDs are: Page1, Page2, Page3 & Page4

我做错了什么?

可能是你遇到了配置问题我在这里创建了一个演示

https://github.com/ShKhan9/PagerShow


let wind = (UIApplication.shared.delegate as! AppDelegate).window!
wind.rootViewController = SetupViewController()