I have a pageviewcontroller that I want to have multiple navigation controllers in. I keep the error type HomeViewController does not conform to 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]
}
}
So my question is. Is there a way to effectively do this and remove this error? Is there a way to control which view controller appears in what order? Becuase there is a certain order in which I want them to appear
Since you have a static list of view controllers that doesn't appear to change, you should be able to do away with conforming to the UIPageControllerDataSource
and simply use setViewControllers(_:direction:animated:completion:)
in viewDidLoad()
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
You might need to change the type of your viewControllerList
to [UIViewController]
This also means that you can completely remove your methods:
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UINavigationController) -> UINavigationController?
and
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UINavigationController) -> UINavigationController? {
If you do want to conform to the UIPageControllerDataSource
, you'd have to have the method signatures be exactly what's required:
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerBefore viewController: UIViewController) -> UIViewController?
and
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController?
Fortunately since UINavigationController is a subclass of UIViewController, the rest of your code should work.
The order of the view controller will be the same as the order you have them in your array returned from viewControllerList