UILabel:在打开Optional值时意外地发现了nil

时间:2017-08-24 14:08:55

标签: ios swift uilabel

免责声明:这不是一个重复的问题,因为类似问题中没有一个建议有助于解决我的问题,而我的问题可能有不同的问题原因;请仔细阅读问题。

我知道这个问题在这里被多次询问过,但在我的案例中没有一个答案有用。

我正在尝试为我的应用创建演练教程屏幕,因此我使用UIPageViewControllerUIViewController来显示教程内容。 我一直在读一本书中的教程。当我进行书籍练习时,一切都正常,但是当涉及到我自己的项目时,我得到了

  

"在展开可选值"

时意外发现nil
UILabel(s)的

错误。 我有三个教程屏幕,但由于它们具有相同的布局,我使用一个视图控制器来显示所有三个屏幕,并且使用3个视图控制器似乎是多余的。

以下是我的视图控制器类的代码:

class TutorialContentViewController: UIPageViewController {

    // MARK: - Properties

    @IBOutlet weak var tutorialHeadingLabel: UILabel!
    @IBOutlet weak var tutorialImageViewController: UIImageView!
    @IBOutlet weak var tutorialDescriptionLabel: UILabel!

    var index = 0
    var heading = ""
    var imageFile = ""
    var tutorialDescription = ""


    override func viewDidLoad() {
        super.viewDidLoad()

        tutorialHeadingLabel.text = heading
        tutorialImageViewController.image = UIImage(named: imageFile)
        tutorialDescriptionLabel.text = tutorialDescription
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

以下是UIPageViewController的代码:

class TutorialPageViewController: UIPageViewController, UIPageViewControllerDataSource {

    // MARK: - Properties

    let tutorialHeadings = ["Heading 1", "Heading 2", "Heading 3"]
    let tutorialImages = ["image_1", "image_2", "image_3"]
    let tutorialDescriptions = ["Description 1", "Description 2", "Description 3"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set the data source to itself
        dataSource = self

        // Create the first walkthrough screen
        if let firstTutorialViewController = getContentViewController(at: 0) {
            setViewControllers([firstTutorialViewController], direction: .forward, animated: true, completion: nil)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Data Source

    public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        var index = (viewController as! TutorialContentViewController).index
        index -= 1

        return getContentViewController(at: index)
    }

    public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        var index = (viewController as! TutorialContentViewController).index
        index += 1

        return getContentViewController(at: index)
    }

    // MARK: - Private Methods

    private func getContentViewController(at index: Int) -> TutorialContentViewController? {
        if index < 0 || index >= tutorialHeadings.count {
            return nil
        }

        // Create a new view controller and pass required data
        if let contentViewController = storyboard?.instantiateViewController(withIdentifier: "TutorialContentViewController") as? TutorialContentViewController {
            contentViewController.heading = tutorialHeadings[index]
            contentViewController.imageFile = tutorialImages[index]
            contentViewController.tutorialDescription = tutorialDescriptions[index]
            contentViewController.index = index

            return contentViewController
        }

        return nil
    }
}

然后在应用程序的初始视图控制器中,我在viewDidAppear(:)

中添加了这段代码
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    if let tutorialPageViewController = storyboard?.instantiateViewController(withIdentifier: "TutorialPageViewController") as? TutorialPageViewController {
        present(tutorialPageViewController, animated: true, completion: nil)
    }
}

然而,如前所述,它给了我

  

&#34;在展开可选值&#34;

时意外发现nil
UILabelTutorialContentViewControllerviewDidLoad()的{​​{1}}错误,<{1}}。

我检查了连接,它们都设置正确;在堆栈溢出的类似问题中,有人建议使用storyboard.instantiateViewController(:)方法,而不是做这样的事情:var viewController = SomeViewController(),我也做了。

另一个建议是在UILabel方法中填充viewDidLoad而不是其他地方,我就是这么做的。这些是有助于解决问题的类似问题的建议,但就我而言,我的代码与这些建议完全相同。所以,我认为问题的原因是别的。

请帮忙

0 个答案:

没有答案