Wei Jay Wei Jay - 2 months ago 4x
iOS Question

How to sync index between view controllers

I'd like to implement something like PageMenu. Currently, I have a root view controller with 2 containers inside, named headerViewController and containerViewController.
enter image description here
I've set everything up but I have no idea how to synchronize the behaviour between these two view controllers.

- How can I send my index from UICollectionViewController to UIPageViewController?

- How can I change the index in UICollectionViewController when UIPageViewController did Scroll?


var headerViewController: PageMenuViewController?
var containerViewController: PageViewController?

override func awakeFromNib() {
pages = [Pages(name: "Page1", selected: true, url: photo1), Pages(name: "Page2", url: video1)]

override func viewDidLoad() {

// MARK: Navigation
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "rootViewSegue" {
containerViewController = segue.destination as? PageViewController
containerViewController!.controllerArray = pages
} else if segue.identifier == "headerViewSegue" {
headerViewController = segue.destination as? PageMenuViewController
headerViewController!.dataArray = pages


func jumpToPage(_ index: Int) {
if index < orderedViewControllers.count {
setViewControllers([orderedViewControllers[index]], direction: .forward, animated: true, completion: nil)

headerViewController (UICollectionViewController)

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// Sync index with containerViewController
// How to use jumpToPage ??

enter image description here


You can use the delegation pattern, rootViewController will implement the protocols containerViewControllerDelegate and headerViewControllerDelegate so you can pass the data through these delegates.

Let me know if you need an example.