我有Swip手势将PageViewController转换为ScrollViewController
它可以工作,但是我想修改它并使用按钮滚动。

问题:情节提要中有三个ViewControllers,但我不知道如何调用父类PageViewController并要求将当前视图更改为下一个视图。

例如,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 = $0 as? ViewController {
                controller.pageViewDelegate = self
            }
        }
    }
    func previousPage() {
        //show previous page
    }
    func nextPage() {
        //show next page
    }
}

阅读有关协议here的更多信息。

关于ios - 在Swift中使用UIPageViewController来水平滚动ViewController的按钮,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59426632/

10-14 03:52