【问题标题】:How to ensure I'm not accessing outlets before they're loaded in如何确保在加载之前我没有访问插座
【发布时间】:2017-02-20 04:54:13
【问题描述】:

我正在制作一个交互式故事应用程序。 PageController: UIViewController 控制故事,第一页加载正常,但是当我进行选择时,应用程序崩溃并出现致命错误 Unexpected found nil when unwrapping。上线:

firstChoiceButton.setTitle(firstChoice.title, for: UIControlState())

查了一下,好像有一个可选的...也许它没有与我的 Story.swift 文件通信?

也许是@IBOutlet firstChoiceButton?但是我认为我所有的@IBOutlets/Actions 都正确连接了...帮助?

import UIKit
class PageController: UIViewController {

    @IBOutlet weak var storyLabel: UILabel!
    @IBOutlet weak var firstChoiceButton: UIButton!
    @IBOutlet weak var secondChoiceButton: UIButton!
    @IBOutlet weak var backdrop2: UIImageView!

    var page: Page?

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)}

    init(page: Page) {
        self.page = page
        super.init(nibName: nil, bundle: nil)}

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


    if let page = page {

            let attributedString = NSMutableAttributedString(string:    page.story.text)

            let paragraphStyle = NSMutableParagraphStyle()

            paragraphStyle.lineSpacing = 5

            attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, attributedString.length))

    if let firstChoice = page.firstChoice {

        firstChoiceButton.setTitle(firstChoice.title, for: UIControlState())

            firstChoiceButton.addTarget(self, action: #selector(PageController.loadFirstChoice), for: .touchUpInside)

            } else {

                firstChoiceButton.setTitle("Meep", for: UIControlState())
                firstChoiceButton.addTarget(self, action: #selector(PageController.playAgain), for: .touchUpInside)
            }

            if let secondChoice = page.secondChoice {

                secondChoiceButton.setTitle(secondChoice.title, for: UIControlState())

               secondChoiceButton.addTarget(self, action: #selector(PageController.loadSecondChoice), for: .touchUpInside)
            }
        storyLabel.attributedText = attributedString
    }
    }

    @IBAction func loadFirstChoice() {
        if let page = page, let firstChoice = page.firstChoice {
            let nextPage = firstChoice.page
            let pageController = PageController(page: nextPage)

            //    playSound(nextPage.story.soundEffectURL as URL)
            navigationController?.pushViewController(pageController, animated: true)
        }
    }

    @IBAction func loadSecondChoice() {
        if let page = page, let secondChoice = page.secondChoice {
            let nextPage = secondChoice.page
            let pageController = PageController(page: nextPage)

          //  playSound(nextPage.story.soundEffectURL as URL)
            navigationController?.pushViewController(pageController, animated: true)
        }
    }

    @IBAction func playAgain() {
        navigationController?.popToRootViewController(animated: true)
    }
}

【问题讨论】:

    标签: swift null fatal-error optional unwrap


    【解决方案1】:

    您应该以编程方式实例化视图控制器并将其推送到导航控制器堆栈。这里有一些代码可以帮助您入门。

    func loadFirstChoice() {
            if let page = page, let firstChoice = page.firstChoice {
                let nextPage = firstChoice.page
                //let pageController = PageController(page: nextPage)
    
                //    playSound(nextPage.story.soundEffectURL as URL)
                let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    
                if let newPage = mainStoryboard.instantiateViewController(withIdentifier: "newpage") as? PageController { //newpage is the storyboard id of the view controller
                    newPage.page = nextPage
                    navigationController?.pushViewController(newPage, animated: true)
                }
            }
        }
    

    【讨论】:

    • 感谢您的宝贵时间!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 2014-05-02
    • 2019-10-22
    相关资源
    最近更新 更多