0

I know this questions has been answered before on here but I was unable to find an answer using Swift. Currently I have a pageviewcontroller embedded in a container view. The container view is inside the same viewcontroller as the pagecontrol. I'm using a delegate to get the pagecontrol to change when paging back and forth but currently the pages do not change when tapping on the pagecontrol dots. How can I do this in Swift?

The answers I have found on here answer the question but not with a pageviewcontroller.

user1715916
  • 333
  • 5
  • 13
  • Here's several answers using Swft: http://stackoverflow.com/questions/13854222/how-can-i-change-the-page-on-clicking-the-dots-of-uipagecontrol – Putz1103 Sep 09 '16 at 16:01
  • Those answers involve using a collectionview. I'm using a pageviewcontroller. – user1715916 Sep 09 '16 at 17:08
  • Quote from those answers: "Clicking the `UIPageControl`'s dots moves your current view to the left or to the right." – Putz1103 Sep 09 '16 at 18:25
  • Here is a quote from the sentence before. "This is a Swift 2 Solution for UICollectionView" – user1715916 Sep 09 '16 at 18:56
  • Good grief, man. I led you to water... Drink. The other answer to that question says absolutely nothing about UICollectionView, only UIPageControl. It has 50 upvotes (so it's a relatively good answer) and is exactly what you are asking here. `How do I change the page when clicking on the UIPageControl dots?` – Putz1103 Sep 09 '16 at 19:28
  • I appreciate it but that answer is in objective-c not swift and it involves a scroll view. – user1715916 Sep 09 '16 at 19:39

1 Answers1

3

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
}
user1715916
  • 333
  • 5
  • 13