使用 PageController 移动到下一个 ViewMontroller swift

Moving to next ViewMontroller with PageController swift

我有一个页面 viewcontroller,我在其中显示了一些项目,在主 ViewController 中,我希望能够使用按钮移至下一页。我已经将 viewcontrollers 添加到一个数组中,但是如何创建一个将在下一步和后退按钮中调用的函数让我感到困惑。

下面是我的代码

private var pageController: UIPageViewController!

    lazy var controllers: [UIViewController] = {
        let locVC = LocationVC()
        locVC.delegate = self
        let typeVC = TypeVC()
        typeVC.delegate = self
        return [locVC,
                typeVC]
    }() 

private func setupPageController() {

    self.pageController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
    self.pageController.dataSource = self
    self.pageController.delegate = self
    pageController.view.translatesAutoresizingMaskIntoConstraints = false

    addChild(pageController)
    view.addSubview(pageController.view)
    NSLayoutConstraint.activate([
        pageController.view.topAnchor.constraint(equalTo: navView.bottomAnchor),
        pageController.view.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
        pageController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        pageController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        ])
    pageController?.setViewControllers([controllers[0]], direction: .forward, animated: false)

}

func viewControllerAtIndex(index: Int) -> UIViewController? {
        if controllers.count == 0 || index >= controllers.count {
            return nil
        }

        return UIViewController()
    }

    func backBtnClicked() {

        guard currentIndex - 1 >= 0 else { return }
        currentIndex -= 1
        guard let startingViewController = viewControllerAtIndex(index: currentIndex) else { return }
        pageController.setViewControllers([startingViewController], direction: .reverse, animated: false)
    }

所有 viewcontrollers 都有一个变量,它是 var pageIndex: Int = 0 我认为这是用来检测当前页面的变量

您需要(e.x 在寻呼机中转到第一个 vc )

pageController.setViewControllers([controllers[index]], direction: .forward, animated: true, completion: nil)