Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

rootViewController

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() {
    super.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
    }
}

containerViewController(UIPageViewController)

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

like image 683
Willjay Avatar asked Feb 14 '26 10:02

Willjay


1 Answers

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.

like image 83
Marco Santarossa Avatar answered Feb 16 '26 22:02

Marco Santarossa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!