I'm fairly new to Swift and so I'm having a pretty basic issue but I can't seem to find an answer to it. I have a ViewController with a container view. A Page Controller is embedded in the container view and I am able to scroll through the different pages but the issue I'm having is I want to change the pages based on a button I press in the ViewController.
This is my code for my PageController:
var pageIndex = 0
class FirstViewController: UIViewController {
@IBOutlet weak var FirstButton: UIButton!
@IBOutlet weak var SecondButton: UIButton!
@IBOutlet weak var currentView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func FirstButtonAction(_ sender: Any) {
SecondButton.backgroundColor = UIColor.black
SecondButton.setTitleColor(.white, for: .normal)
FirstButton.backgroundColor = UIColor.white
FirstButton.setTitleColor(.black, for: .normal)
pageIndex = 0
}
@IBAction func SecondButtonAction(_ sender: Any) {
FirstButton.backgroundColor = UIColor.black
FirstButton.setTitleColor(.white, for: .normal)
SecondButton.backgroundColor = UIColor.white
SecondButton.setTitleColor(.black, for: .normal)
pageIndex = 1
}
}
class PageViewController: UIPageViewController, UIPageViewControllerDataSource {
lazy var viewControllerList:[UIViewController] = {
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc1 = sb.instantiateViewController(withIdentifier: "FirstVC")
let vc2 = sb.instantiateViewController(withIdentifier: "SecondVC")
return [vc1, vc2]
}()
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let vcIndex = viewControllerList.firstIndex(of: viewController) else {return nil}
pageIndex = vcIndex - 1
guard pageIndex >= 0 else {return nil}
guard viewControllerList.count > 0 else {return nil}
return viewControllerList[pageIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let vcIndex = viewControllerList.firstIndex(of: viewController) else {return nil}
pageIndex = vcIndex + 1
guard viewControllerList.count != pageIndex else {return nil}
guard viewControllerList.count > pageIndex else {return nil}
return viewControllerList[pageIndex]
}
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
if let firstViewController = viewControllerList.first {
self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
}
}
Most of this is just Google searching and YouTube videos so if my code looks choppy anywhere else, please tell me how to fix it. Any recommendations?
Images to show kind of the result I'm looking for (pretty simple I guess):
UPDATE: I tried adding this but this didn't work:
@IBAction func SecondButtonAction(_ sender: Any) {
guard let currentViewController =
PageViewController().viewControllerList.first else { return }
guard let nextViewController =
PageViewController().dataSource?.pageViewController(PageViewController(), viewControllerAfter: currentViewController) else { return }
PageViewController().setViewControllers([nextViewController], direction: .forward, animated: false, completion: nil)
}