I have a UIPageViewController set up containing 5 view controllers that I created in storyboard. The pages change just fine with a swipe gesture, but I want the page to change after the user taps a button within one of the view controllers. Here's my code:
// PageVC.swift
import Foundation
import UIKit
class PageVC: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
lazy var VCArr: [UIViewController] = {
return [self.VCInstance(name: "OB1"),
self.VCInstance(name: "OB2"),
self.VCInstance(name: "OB3"),
self.VCInstance(name: "OB4"),
self.VCInstance(name: "OB5"),
self.VCInstance(name: "OB6")]
}()
private func VCInstance(name: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)
}
override public func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
if let OB1 = VCArr.first {
setViewControllers([OB1], direction: .forward, animated: true, completion: nil)
}
}
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?{
guard let viewControllerIndex = VCArr.index(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return VCArr.last
}
guard VCArr.count > previousIndex else {
return nil
}
return VCArr[previousIndex]
}
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?{
guard let viewControllerIndex = VCArr.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
guard nextIndex < VCArr.count else {
return VCArr.first
}
guard VCArr.count > nextIndex else {
return nil
}
return VCArr[nextIndex]
}
public func presentationCount(for pageViewController: UIPageViewController) -> Int{
return VCArr.count
}
public func presentationIndex(for pageViewController: UIPageViewController) -> Int{
guard let OB1 = viewControllers?.first,
let OB1Index = VCArr.index(of: OB1) else {
return 0
}
return OB1Index
}
public func nextPageWithIndex(index: Int)
{
let nextVC = VCArr[index]
setViewControllers([nextVC], direction: .forward, animated: true, completion: nil)
}
}
I call the nextPageWithIndex
method here in an IBAction in one of my view controllers:
// OB1.swift
import UIKit
class OB1: UIViewController {
@IBOutlet var getStartedColorChange: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func getStartedBtn(_ sender: Any) {
let getStarted = PageVC()
getStarted.nextPageWithIndex(index: 2)
}
}
The button works, as I am able to print from it, but the page isn't changing. What do I need to change in order to change pages using the button?
The problem is the new instance of PageVC() in your code:
let getStarted = PageVC()
You must get the reference to parent controller that already created!
@IBAction func getStartedBtn(_ sender: Any) {
let getStarted = self.parent as! PageVC
getStarted.nextPageWithIndex(index: 2)
}