I currently have a button
that currently displays a UIPageViewController
as an overlay on the homescreen. The user can always click on it and it would still display this ViewControllers
embedded in a UIPageViewController
. However at the moment, when the user clicks on the button to display the overlay and closes it and opens it again. The last page index / state of the last page is saved and displayed again. What i would like to do is to always display the first ViewController
in the UIPageviewController
. I have tried different ways but haven't had any luck.
Here is the screen capture that shows how the current mechanism works
HomeHelpViewController that contains the UIPageViewController
class HomeHelpViewController: UIPageViewController, UIPageViewControllerDataSource {
var helpPages: [UIViewController] = []
var currentIndex: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
let helpStoryboard = UIStoryboard(name: "HomeHelp", bundle: nil)
for scanIdentifier in ["homeHelpVC1","homeHelpVC2","homeHelpVC3","homeHelpVC4"] {
let scanVC = helpStoryboard.instantiateViewController(withIdentifier: scanIdentifier)
self.helpPages.append(scanVC)
}
self.dataSource = self
self.setViewControllers([self.helpPages.first!], direction: .forward, animated: true)
}
//MARK:- UIPageViewControllerDataSource
func pageViewController(_ pPageViewController: UIPageViewController, viewControllerBefore pViewController: UIViewController) -> UIViewController? {
self.currentIndex = self.helpPages.firstIndex(of: pViewController)!
if self.currentIndex > 0 {
return self.helpPages[self.currentIndex - 1]
}
return nil
}
func pageViewController(_ pPageViewController: UIPageViewController, viewControllerAfter pViewController: UIViewController) -> UIViewController? {
self.currentIndex = self.helpPages.firstIndex(of: pViewController)!
if self.currentIndex < (self.helpPages.count - 1) {
return self.helpPages[self.currentIndex + 1]
}
return nil
}
func presentationCount(for pPageViewController: UIPageViewController) -> Int {
return self.helpPages.count
}
func presentationIndex(for pPageViewController: UIPageViewController) -> Int {
return self.currentIndex
}
}
HomePageViewController that has the button display the UIPageViewController as an overlay
class HomePageViewController: UIViewController {
@IBOutlet weak var helpOverlay: BaseOverlayView!
@IBAction func helpButton(_ pSender: Any) {
self.helpButton.setImage(AppDelegate.helpButtonImage(tutorial: true), for: .normal)
self.helpOverlay.showOnView(self.view)
}
@IBAction func closeHelp(_ pSender: UIButton) {
self.helpOverlay.removeFromView()
self.helpButton.setImage(AppDelegate.helpButtonImage(tutorial: false), for: .normal)
}
}
declare a variable to access your HomeHelpViewController inside your HomePageViewController
class HomePageViewController: UIViewController {
weak var homePageController : HomeHelpViewController?
@IBOutlet weak var helpOverlay: BaseOverlayView!
@IBAction func helpButton(_ pSender: Any) {
if let homePageController = homePageController {
homePageController.setViewControllers([homePageController.helpPages.first!], direction: .forward, animated: true)
}
self.helpButton.setImage(AppDelegate.helpButtonImage(tutorial: true), for: .normal)
self.helpOverlay.showOnView(self.view)
}
@IBAction func closeHelp(_ pSender: UIButton) {
self.helpOverlay.removeFromView()
self.helpButton.setImage(AppDelegate.helpButtonImage(tutorial: false), for: .normal)
}
}
inside this loop assign your HomeHelpViewController
for scanIdentifier in ["homeHelpVC1","homeHelpVC2","homeHelpVC3","homeHelpVC4"] {
let scanVC = helpStoryboard.instantiateViewController(withIdentifier: scanIdentifier) as? HomePageViewController
scanVc?.homePageController = self
self.helpPages.append(scanVC)
}