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

Posted

技术标签:

【中文标题】如何跳回 UIPageviewController 中页面的第一个索引【英文标题】:How to jump back to a first index of a page in UIPageviewController 【发布时间】:2019-03-06 09:11:32 【问题描述】:

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

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

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)
        

【问题讨论】:

【参考方案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)
            

【讨论】:

如何在helpbutton action 中访问self.setViewControllers([self.helpPages.first!], direction: .forward, animated: true) 。这是另一个viewController【参考方案2】:

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

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

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

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

【讨论】:

以上是关于如何跳回 UIPageviewController 中页面的第一个索引的主要内容,如果未能解决你的问题,请参考以下文章

如何从属于 UIPageViewController 的 UIViewController 之一更改 UIPageViewController?

如何删除 UIPageViewController 中的 previousViewControllers

如何更改 UIPageViewController 中的按钮名称

如何快速阻止 UIPageViewController 的滑动手势?

如何让 UIPageViewController 自动切换控制器

如何在 UIPageViewController 和孩子之间正确传递对象