Swift-RootPageViewController,添加来自不同ViewController的按钮

时间:2019-04-25 17:18:22

标签: ios swift

我已经设置好 RootPageViewController ,我所需要的只是添加UIButtons。我已经从首先显示的UIViewController中添加了按钮及其功能。

我如何还能从f.e.添加UIButtons第三UIViewController并为其分配功能? 就像首先显示的 secondViewController 中的按钮一样:secondViewController.buttonAdd.action = #selector(addData(_:))

如果我仅使用firstViewController.buttonBack.action = #selector(backData(_:))-我会收到一条错误消息:

  

线程1:致命错误:在隐式展开一个可选值时意外发现nil

我认为那是因为,没有if let firstViewController ...

但是假设我要添加:

if let firstViewController = viewControllerList.first as? TimelineViewController {
            self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
            firstViewController.buttonBack.action = #selector(backData(_:))
        }

在这种情况下,将首先显示哪个视图,然后混合显示下一个视图的顺序。

我愿意征求建议和更好的方法,我只是希望它能起作用,


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
        if let secondViewController = viewControllerList[1] as? MainViewController {
            self.setViewControllers([secondViewController], direction: .forward, animated: true, completion: nil)
            secondViewController.buttonAdd.action = #selector(addData(_:))
        }
    }

    @objc func addData(_ sender: Any) {
        if let thirdViewController = viewControllerList.last {
            self.setViewControllers([thirdViewController], direction: .forward, animated: true, completion: nil)
        }
    }

    @objc func backData(_ sender: Any) {
        let secondViewController = viewControllerList[1]
        self.setViewControllers([secondViewController], direction: .forward, animated: true, 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]
    }

}

1 个答案:

答案 0 :(得分:0)

解决方案是通过任何UIViewControllers按钮操作来调用它:

// get parent view controller
    let parentVC = self.parent as! UserDetailsPVC

    // change page of PageViewController
    parentVC.setViewControllers([parentVC.pages[1]], direction: .forward, animated: true, completion: nil)