如何在 UIPageviewController 中跳回到页面的第一个索引

How to jump back to a first index of a page in UIPageviewController

我目前有一个 button,目前在主屏幕上显示 UIPageViewController 作为叠加层。用户可以随时单击它,它仍然会显示嵌入在 UIPageViewController 中的 ViewControllers。但是目前,当用户单击按钮以显示叠加层并将其关闭并再次打开时。最后一页的最后一页索引/状态被保存并再次显示。我想做的是始终在 UIPageviewController 中显示第一个 ViewController。我尝试了不同的方法,但没有任何运气。

这是显示当前机制如何工作的屏幕截图

https://gph.is/g/Z2QlyDa

包含 UIPageViewController 的 HomeHelpViewController

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)
        }
}

在 HomePageViewController 中声明一个变量来访问 HomeHelpViewController

    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)
            }
    }

在此循环中分配您的 HomeHelpViewController

for scanIdentifier in ["homeHelpVC1","homeHelpVC2","homeHelpVC3","homeHelpVC4"] {
                let scanVC = helpStoryboard.instantiateViewController(withIdentifier: scanIdentifier) as? HomePageViewController
    scanVc?.homePageController = self
                self.helpPages.append(scanVC)
            }

这可能是因为 UIPageViewController 没有被正确关闭。

func dismiss(animated flag: Bool, completion: (() -> Void)? = nil)

您可以参考这里的文档:https://developer.apple.com/documentation/uikit/uiviewcontroller/1621505-dismiss

这有效地解除了 UIPageViewController 的分配,以便一旦按下按钮,它将在第一页重新初始化。