My situation was different from the answers provided in the link. I had a viewpagecontroller in container view and followed this tutorial and the second one where there is an explanation given for the page control.
So I went to the parent viewcontroller with the container view and created a global variable:
var pagerController: MainPageViewController!
Then used:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "PagerContainerSegue") {
//This is used to reference the paging left and right functions
let childViewController = segue.destination as! MainPageViewController
pagerController = childViewController
}
}
Then I added an outlet for the pagecontrol when the value changed:
@IBAction func pageControlValueChange(_ sender: AnyObject) {
//setViewControllers comes from the pageviewcontroller
if(pageIndex == 0){
pagerController.setViewControllers([orderedViewControllers[1]],
direction: .forward,
animated: true,
completion: nil)
pageIndex += 1
}else{
pagerController.setViewControllers([orderedViewControllers[0]],
direction: .reverse,
animated: true,
completion: nil)
pageIndex -= 1
}
}
The pageIndex is assigned here:
func mainPageViewController(_ mainPageViewController: MainPageViewController,
didUpdatePageIndex index: Int) {
pageIndex = index
pageControl.currentPage = index
}