Swift - 单击按钮时更改 UIPageViewController 视图

Tim*_*Tim 1 ios uipageviewcontroller swift

我正在使用 an UIPageViewController,我的目的是在单击UIButton其中一个UIViewController. 我已经用谷歌搜索了我的问题并找到了一些类似的线索,但我无法弄清楚答案。

例如,我的第二个按钮UIViewController vc2应将视图更改为vc3

根页面视图控制器

import UIKit

class RootPageViewController: UIPageViewController, UIPageViewControllerDataSource {

    lazy var viewControllerList:[UIViewController] = {

        let sb = UIStoryboard(name: "Main", bundle: nil)

        let vc1 = sb.instantiateViewController(withIdentifier: "timelineView")
        let vc2 = sb.instantiateViewController(withIdentifier: "mainView")
        let vc3 = sb.instantiateViewController(withIdentifier: "addView")

        return [vc1, vc2, vc3]
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.dataSource = self

        let secondViewController = viewControllerList[1]
        self.setViewControllers([secondViewController], direction: .forward, animated: false, completion: nil)

    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let vcIndex = viewControllerList.firstIndex(of: viewController) else { return nil }

        let previousIndex = vcIndex - 1
        guard previousIndex >= 0 else { return nil }

        guard viewControllerList.count > previousIndex else { return nil }

        return viewControllerList[previousIndex]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        guard let vcIndex = viewControllerList.firstIndex(of: viewController) else { return nil }

        let nextIndex = vcIndex + 1

        guard viewControllerList.count != nextIndex else { return nil }

        guard viewControllerList.count > nextIndex else { return nil }

        return viewControllerList[nextIndex]
    }

}
Run Code Online (Sandbox Code Playgroud)


第二个 ViewController 内的按钮

@IBAction func addData(_ sender: Any) {


    }
Run Code Online (Sandbox Code Playgroud)

Raj*_*r R 5

RootPageViewControllerviewDidLoad添加第二视图控制器按钮的目标self,并添加addData的方法RootPageViewController。您可以使用setViewControllers并移动到该方法中的最后一个视图控制器

override func viewDidLoad() {
    super.viewDidLoad()

    self.dataSource = self
    if let secondViewController = viewControllerList[1] as? SecondViewController {
        self.setViewControllers([secondViewController], direction: .forward, animated: false, completion: nil)
        secondViewController.button.action = #selector(addData(_:))
    }
}
@IBAction func addData(_ sender: Any) {
    if let thirdViewController = viewControllerList.last {
        self.setViewControllers([thirdViewController], direction: .forward, animated: false, completion: nil)
    }
}
Run Code Online (Sandbox Code Playgroud)

或者

在 MainViewControlle 中保留 addData 方法并像这样向其添加协议

protocol MainVCDelegate {
    func buttonPlusTapped()
}
class MainViewController: UIViewController {

    @IBOutlet var buttonPlus: UIBarButtonItem!

    var delegate: MainVCDelegate?
    @IBAction func addData(_ sender: Any) {
        delegate?.buttonPlusTapped()
    }
}
Run Code Online (Sandbox Code Playgroud)

在 RootPageViewController 中确认此委托并添加委托方法

class RootPageViewController: UIPageViewController, UIPageViewControllerDataSource, MainVCDelegate {

    func buttonPlusTapped() {
        if let thidViewController = viewControllerList.last {
            self.setViewControllers([thidViewController], direction: .forward, animated: false, completion: nil)
        }
    }

    lazy var viewControllerList:[UIViewController] = {

        let sb = UIStoryboard(name: "Main", bundle: nil)

        let vc1 = sb.instantiateViewController(withIdentifier: "timelineView")
        let vc2 = sb.instantiateViewController(withIdentifier: "mainView") as! MainViewController
        vc2.delegate = self
        let vc3 = sb.instantiateViewController(withIdentifier: "addView")
        return [vc1, vc2, vc3]
    }()
}
Run Code Online (Sandbox Code Playgroud)