在 Swift 中使用 UIPageViewController 水平滚动 ViewControllers 的按钮

Buttons to scroll horizontally ViewControllers with UIPageViewController in Swift

我有 PageViewControllerScrollViewController 的滑动手势。 它有效,但我想修改它并使用按钮滚动。

问题:Storyboard里有3个ViewControllers但是不知道如何调用parentclassPageViewController要求把当前视图换成下一个。

例如,ViewController1 按钮 "nextView" -> ViewController2。 在直接调用 ViewController2 的情况下,PageViewController 控件可能会被破坏。正确吗?

ViewController1 的 UIButton 应该用什么代码?

PageViewController代码:

class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

    lazy var orderedVC: [UIViewController] = {
        return [self.newViewController(viewController: "ViewController1"),
                self.newViewController(viewController: "ViewController2"),
                self.newViewController(viewController: "ViewController3")]
    }()

    //Create the PageControl
    var pageControl = UIPageControl()
    func configurePageControl() {
        pageControl = UIPageControl(frame: CGRect(x: 0, y: UIScreen.main.bounds.maxY - 50, width: UIScreen.main.bounds.width, height: 50))
        ...
        self.view.addSubview(pageControl)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.dataSource = self
        if let firstViewController = orderedVC.first {
            setViewControllers([firstViewController],
                               direction: .forward,
                               animated: true,
                               completion: nil)
        }
        self.delegate = self
        configurePageControl()
    }

    func newViewController(viewController: String) -> UIViewController {
        return UIStoryboard(name: "Entry", bundle: nil).instantiateViewController(withIdentifier: viewController)
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController:UIViewController) -> UIViewController? {

        //Get index from the the views list BEFORE
        guard let viewControllerIndex = orderedVC.firstIndex(of: viewController)   else {return nil}
        let previousIndex = viewControllerIndex - 1

        //Limitations of the index
        guard previousIndex >= 0    else {return nil}
        guard orderedVC.count > previousIndex  else {return nil}

        return orderedVC[previousIndex]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController:UIViewController) -> UIViewController? {

        //Get index from the the views list AFTER
        guard let viewControllerIndex = orderedVC.firstIndex(of: viewController)   else {return nil}
        let nextIndex = viewControllerIndex + 1

        //Limitations of the index
        guard orderedVC.count != nextIndex else {return nil}
        guard orderedVC.count > nextIndex  else {return nil}

        return orderedVC[nextIndex]
    }

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousVC: [UIViewController], transitionCompleted completed: Bool) {
        let pageContentViewController = pageViewController.viewControllers![0]
        self.pageControl.currentPage = orderedVC.firstIndex(of: pageContentViewController)!
    }
}

可以使用自定义 protocol 并使您的 PageView 控制器成为视图控制器的委托。

protocol PageViewDelegate {
    func previousPage()
    func nextPage()
}

class ViewController: UIViewController {
    var pageViewDelegate: PageViewDelegate?
    @IBAction func previousButtonTapped(_ sender: Any) {
        pageViewDelegate?.previousPage()
    }
    @IBAction func nextButtonTapped(_ sender: Any) {
        pageViewDelegate?.nextPage()
    }
}

class PageViewController: UIPageViewController, PageViewDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        // After the viewControllers have been setup
        self.viewControllers?.forEach {
            if let controller = [=10=] as? ViewController {
                controller.pageViewDelegate = self
            }
        }
    }
    func previousPage() {
        //show previous page
    }
    func nextPage() {
        //show next page
    }
}

详细了解协议 here