В настоящее время у меня есть button
, который в настоящее время отображает UIPageViewController
в качестве наложения на главном экране. Пользователь всегда может щелкнуть по нему, и он все равно будет отображать этот ViewControllers
, встроенный в UIPageViewController
. Однако в тот момент, когда пользователь нажимает на кнопку, чтобы отобразить оверлей, закрывает его и открывает снова. Индекс последней страницы / состояние последней страницы сохраняется и отображается снова. То, что я хотел бы сделать, это всегда отображать первый ViewController
в UIPageviewController
. Я пробовал разные способы, но мне не повезло.
Вот снимок экрана, который показывает, как работает текущий механизм
https://gph.is/g/Z2QlyDa
HomeHelpViewController, который содержит 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, у которого есть кнопка, отображает UIPageViewController как наложение
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)
}
}