从RootPageViewController调用按钮会导致错误

时间:2019-04-25 12:08:39

标签: ios swift

要更改单击UIBarButtonItem的视图,我试图在RootPageViewController内调用一个按钮。单击后,视图将改变。无论如何,一旦我启动应用程序,就会发生以下错误(只要应用程序未启动,Xcode就不会检测到任何错误):

对于secondViewController.buttonPlus.action = #selector(addData(_:))

  

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

另外,当我尝试print(secondViewController.buttonPlus)时,结果为nil

如果您知道另一种更改视图的方法,请告诉我。

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 {
            secondViewController.buttonPlus.action = #selector(addData(_:))
            self.setViewControllers([secondViewController], direction: .forward, animated: false, completion: nil)
        }

    }

    @objc func addData(_ sender: Any) {
        if let thirdViewController = viewControllerList.last {
            self.setViewControllers([thirdViewController], 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]
    }

}


MainViewController:

class MainViewController: UIViewController {

    @IBOutlet weak var buttonPlus: UIBarButtonItem!

    ...

    override func viewDidLoad() {
        super.viewDidLoad()
        ...
        }

    ````

1 个答案:

答案 0 :(得分:1)

更改两行的顺序:

override func viewDidLoad() {
    super.viewDidLoad()

    self.dataSource = self
    if let secondViewController = viewControllerList[1] as? MainViewController {

        // call this first
        self.setViewControllers([secondViewController], direction: .forward, animated: false, completion: nil)

        // now you have access to buttonPlus
        secondViewController.buttonPlus.action = #selector(addData(_:))

    }

}