0
votes

I have a very specific use case that I am wondering if anyone has dealt with. I want to add a "Continue" button on my first time user experience UIPageViewController. This article (Swift iOS: how to trigger next page using buttons), touches if you want it to be on every page, but I am not planning that broad of use.

I have a UIPageViewController, and 6 viewcontrollers that I currently can navigate through by swiping.

I've tried connecting the button to the view controller as a segue, but that presents it outside of the PageViewController. I've thought of trying to just push a right to left swipe onto the button click, but I couldn't quit figure that out.

Anyone have an idea on how to attack putting a single button on one of the view controllers and have it progress to the next view controller within the UIPageViewController?


This was marked as duplicate, but there are no duplicate cases of this issue on SO. There are few similar, but nothing like it. If there is one, please link it. The currently linked version is an obj-c answer to a similar question with no explanation as to how to link the needed function to the view controller. That answer I can implement as a global button easily, but a single view controllers button is not covered by it.

Thanks


Edit for final code solution.

So this was a two part solution coded in swift 3.

In the uipageviewcontroller data source extension, the func to flip the page was built as follows.

func goNextPage() {
   let cur = self.viewControllers![0]
   let p = self.pageViewController(self, viewControllerAfter: cur)
   self.setViewControllers([p!], direction .forward, animated: true, completion: nil)
  }

The piece in the individual view controller is built as follows

@IBAction func nextPageButtonWasTapped(_ sender: Any) {
  var candidate: UIViewController = self
  while true {
    if let pageViewController = candidate as? MyUIPageViewController {
        pageViewController.goNextPage()
        break
    }
    guard let next = parent else { break }
    candidate = next
  }
}
1
Does your question actually have anything to do with UIPageControl, or is that a mistake in your title?rob mayoff
Sorry about that, edited to uipageviewcontrollerJames Cockerham
See if this answers your question.rob mayoff
I don't think so. I'm not sure how you would connect a button that is inside the viewcontroller back into the uipageviewcontroller. That's the real root of my problem. From there I could use the above answer to call a function in the uipageviewcontroller to forward or reverse, but I can't get there.James Cockerham

1 Answers

1
votes

Walk up the view controller hierarchy to find the page view controller. When you find it, ask it to turn the page:

@IBAction func nextPageButtonWasTapped(_ sender: AnyObject?) {
    var candidate: UIViewController = self
    while true {
        if let pageViewController = candidate as? UIPageViewController {
            candidate.navigateForward()
            break
        }
        guard let next = parentViewController else { break }
        candidate = next
    }
}

Implement navigateForward in an extension on UIPageViewController, based on the Objective-C version from this answer.