ios - Swift - 单击按钮更改 UIPageViewController View

标签 ios swift uipageviewcontroller

我正在使用 UIPageViewController,我的目的是在其 UIViewController 之一中单击 UIButton 时切换当前 View 。我已经用 google 搜索了我的问题并找到了一些类似的线索,但我无法确定答案。

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

RootPageViewController

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]
    }

}


第二个 ViewController 内的按钮

@IBAction func addData(_ sender: Any) {


    }

最佳答案

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

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)
    }
}

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

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

    @IBOutlet var buttonPlus: UIBarButtonItem!

    var delegate: MainVCDelegate?
    @IBAction func addData(_ sender: Any) {
        delegate?.buttonPlusTapped()
    }
}

在 RootPageViewController 中确认这个委托(delegate)并添加委托(delegate)方法

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]
    }()
}

关于ios - Swift - 单击按钮更改 UIPageViewController View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55847188/

相关文章:

ios - 使用 IB_DESIGNABLE obj-c 扩展类

objective-c - UITextField 在 View 出现后丢失 firstResponder

ios - 嵌入 UINavigationController 时如何处理 UIView 区域减少的问题

Objective-C 相当于 Swift 中的 internal(set)

swift - AVPlayer 无法正常工作 - Swift

ios - 收到状态的CA回调,但事件状态队列为空

ios - 使用自动调整单元格设置 UICollectionView 后出现异常

javascript - 徒手绘制时 iOS SVG 滞后

ios - 视口(viewport)属性如何正确使用

ios - 如何检测用户正在输入消息?