更改特定页面UiPageViewController

时间:2016-12-13 00:36:50

标签: ios swift xcode8 uipageviewcontroller

我有一个页面视图控制器(见下文),我想要实现的是从ThirdVC转到FirstVC并且仅从那个!!有功能或按钮,任何人都可以帮助我吗?

在这里,您可以看到我的PageVC的代码

import UIKit

class PageVC: UIPageViewController,UIPageViewControllerDataSource, UIPageViewControllerDelegate{

    lazy var VCArr: [UIViewController] = {
        return [self.VCInstance(name: "FirstVC"), self.VCInstance(name: "SecondVC"), self.VCInstance(name: "ThirdVC")]
          }()

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

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


    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        for view in self.view.subviews {
            if view is UIScrollView {
                view.frame = UIScreen.main.bounds
            } else if view is UIPageControl {
                view.backgroundColor = UIColor.clear
            }
        }
    }

    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 firstViewController = viewControllers?.first, let firstViewControllerIndex = VCArr.index(of: firstViewController) else{
            return 0
        }
        return firstViewControllerIndex
    }

}

1 个答案:

答案 0 :(得分:0)

好的,我在这里找到了解决方案:

How do I change the UIPageViewController from WITHIN one of the UIViewControllers that is part of the UIPageViewController?

Simply i added this function to PageVC:
 func nextPageWithIndex(index: Int)
    {

        let nextWalkthroughVC = VCArr[index]
        setViewControllers([nextWalkthroughVC], direction: .forward, animated: true, completion: nil)

    }

然后在我的函数中添加了ThirdVC视图控制器:

let parent = self.parent as! PageVC

        parent.nextPageWithIndex(index: 0)

其中0是我想要显示的视图控制器的索引

完美无缺!