内置导航控制器的PageViewController

时间:2017-09-06 22:44:15

标签: ios swift uipageviewcontroller

我有一个pageviewcontroller,我想要有多个导航控制器。我保持错误类型HomeViewController不符合UIPageViewController DataSource。

import UIKit
import Firebase
import FirebaseAuth

class HomeViewController: UIPageViewController, UIPageViewControllerDataSource {
    let dropDownLauncher = DropDownLauncher()
        override func viewDidLoad() {
            super.viewDidLoad()
            self.dataSource = self
            if let firstViewController = viewControllerList.first{
                self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
            }
            // setupView()
        }
    lazy var viewControllerList: [UINavigationController] = {
        let layout = UICollectionViewFlowLayout()
        let homeFeedController = HomeFeedController(collectionViewLayout: layout)
        let navController = UINavigationController(rootViewController: homeFeedController)
        let profileView = ProfileeViewController(collectionViewLayout: layout)
        let profileViewNavController = UINavigationController(rootViewController: profileView)
        let searchController = EventSearchController(collectionViewLayout: layout)
        let searchNavController = UINavigationController(rootViewController: searchController)

        return [navController,profileViewNavController,searchNavController]
    }()

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UINavigationController) -> UINavigationController? {
        guard let vcIndex = viewControllerList.index(of: viewController) else{
            return nil
        }
        let previousIndex = vcIndex - 1
        guard previousIndex >= 0 else {
            return nil
        }
        guard viewControllerList.count > previousIndex else{
            return nil
        }
        return viewControllerList[previousIndex]
    }

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

        guard let vcIndex = viewControllerList.index(of: viewController) else{
            return nil
        }
        let nextIndex = vcIndex + 1
        guard viewControllerList.count != nextIndex else{
            return nil
        }
        guard viewControllerList.count > nextIndex else{
            return nil
        }

        return viewControllerList[nextIndex]
        }

}

所以我的问题是。 有没有办法有效地做到这一点并删除此错误? 有没有办法控制哪个视图控制器以什么顺序出现? 因为我希望它们出现在某种顺序

1 个答案:

答案 0 :(得分:0)

由于您有一个静态的视图控制器列表似乎没有变化,因此您应该能够不再遵守UIPageControllerDataSource而只需在viewDidLoad()中使用setViewControllers(_:direction:animated:completion:)

override func viewDidLoad() {
    super.viewDidLoad()
    self.dataSource = self
    self.setViewControllers(self.viewControllerList, direction: .forward, animated: true, completion: nil)
}

https://developer.apple.com/documentation/uikit/uipageviewcontroller/1614087-setviewcontrollers

您可能需要将viewControllerList的类型更改为[UIViewController]

这也意味着您可以完全删除您的方法:

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

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

如果您确实要符合UIPageControllerDataSource,则必须让方法签名完全符合要求:

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

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

幸运的是,由于UINavigationController是UIViewController的子类,因此其余代码应该可以工作。

视图控制器的顺序与从viewControllerList

返回的数组中的顺序相同