为啥我的 UIPageViewController 中的页面没有改变?

Posted

技术标签:

【中文标题】为啥我的 UIPageViewController 中的页面没有改变?【英文标题】:Why is the page not changing in my UIPageViewController?为什么我的 UIPageViewController 中的页面没有改变? 【发布时间】:2017-09-24 16:57:00 【问题描述】:

我设置了一个 UIPageViewController,其中包含我在情节提要中创建的 5 个视图控制器。页面可以通过滑动手势很好地更改,但我希望在用户点击其中一个视图控制器中的按钮后更改页面。这是我的代码:

//  PageVC.swift

import Foundation
import UIKit

class PageVC: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate 

    lazy var VCArr: [UIViewController] = 
        return [self.VCInstance(name: "OB1"),
                self.VCInstance(name: "OB2"),
                self.VCInstance(name: "OB3"),
                self.VCInstance(name: "OB4"),
                self.VCInstance(name: "OB5"),
                self.VCInstance(name: "OB6")]
    ()

    private func VCInstance(name: String) -> UIViewController 
        return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)

    

    override public func viewDidLoad() 
        super.viewDidLoad()
        self.dataSource = self
        self.delegate = self
        if let OB1 = VCArr.first 
            setViewControllers([OB1], direction: .forward, animated: true, completion: nil)
        
    

    public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
        guard let viewControllerIndex = VCArr.index(of: viewController) else 
            return nil
        

        let previousIndex = viewControllerIndex - 1

        guard previousIndex >= 0 else 
            return VCArr.last
        

        guard VCArr.count > previousIndex else 
            return nil
        

        return VCArr[previousIndex]
    

    public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
        guard let viewControllerIndex = VCArr.index(of: viewController) else 
            return nil
        

        let nextIndex = viewControllerIndex + 1

        guard nextIndex < VCArr.count else 
            return VCArr.first
        

        guard VCArr.count > nextIndex else 
            return nil
        

        return VCArr[nextIndex]
    

    public func presentationCount(for pageViewController: UIPageViewController) -> Int

        return VCArr.count

        

    public func presentationIndex(for pageViewController: UIPageViewController) -> Int

        guard let OB1 = viewControllers?.first,
            let OB1Index = VCArr.index(of: OB1) else 
                return 0

        

        return OB1Index
    

    public func nextPageWithIndex(index: Int)
    
        let nextVC = VCArr[index]
        setViewControllers([nextVC], direction: .forward, animated: true, completion: nil)

    



我在其中一个视图控制器的 IBAction 中调用 nextPageWithIndex 方法:

//  OB1.swift

import UIKit

class OB1: UIViewController 
    @IBOutlet var getStartedColorChange: UIButton!


    override func viewDidLoad() 
        super.viewDidLoad()



    

    @IBAction func getStartedBtn(_ sender: Any) 

        let getStarted = PageVC()
        getStarted.nextPageWithIndex(index: 2)

    


该按钮有效,因为我可以从中打印,但页面没有改变。为了使用按钮更改页面,我需要更改哪些内容?

【问题讨论】:

【参考方案1】:

问题是您的代码中的 PageVC() 的新实例:

let getStarted = PageVC()

您必须获得对已经创建的父控制器的引用!

  @IBAction func getStartedBtn(_ sender: Any) 
        let getStarted = self.parent as! PageVC
        getStarted.nextPageWithIndex(index: 2)
    

【讨论】:

以上是关于为啥我的 UIPageViewController 中的页面没有改变?的主要内容,如果未能解决你的问题,请参考以下文章

UIPageViewController 手势覆盖我的 UITableView

如何让 UIPageViewController 知道我的异步下载何时完成?

Swift - 单击按钮更改 UIPageViewController 视图

UIPageViewController:添加多个视图控制器时应用程序崩溃

UIPageViewController 在我的 UIImage 底部创建一个边框以及如何摆脱它

在几秒钟内重新加载 UiPageViewController