Search code examples
swiftnullfatal-erroroption-typeunwrap

How to ensure I'm not accessing outlets before they're loaded in


I'm making an interactive story app. The PageController: UIViewController controls the story, the first page loads fine but when I make a selection, app crashes with the fatal error Unexpectedly found nil when unwrapping. on line:

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

Looking it up, it seems like there's an optional... maybe it's not communicating with my Story.swift file?

Maybe It's the @IBOutlet firstChoiceButton? but all of my @IBOutlets/Actions are hooked up right I think... Help?

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

Solution

  • You should instantiate the view controller programatically and push it to the navigation controller stack. Here is some code to help you get started.

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