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

时间:2019-03-06 09:11:32

标签: ios swift uipageviewcontroller uipagecontrol

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

以下是显示当前机制工作原理的屏幕截图

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

2 个答案:

答案 0 :(得分:1)

声明一个变量以访问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)
            }

答案 1 :(得分:0)

这可能是因为未正确关闭UIPageViewController。

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

您可以在此处参考文档:https://developer.apple.com/documentation/uikit/uiviewcontroller/1621505-dismiss

这有效地取消了UIPageViewController的分配,因此一旦按下按钮,它将在第一页重新初始化。