Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何确保I';我不会在他们';重新加载_Swift_Null_Fatal Error_Optional_Unwrap - Fatal编程技术网

Swift 如何确保I';我不会在他们';重新加载

Swift 如何确保I';我不会在他们';重新加载,swift,null,fatal-error,optional,unwrap,Swift,Null,Fatal Error,Optional,Unwrap,我正在制作一个互动故事应用程序。PageController:UIViewController控制故事,第一页加载良好,但当我进行选择时,应用程序崩溃,在展开时意外发现零致命错误。在线: firstChoiceButton.setTitle(firstChoice.title, for: UIControlState()) 查找它,似乎有一个可选的。。。也许它没有和我的Story.swift文件通信 也许是@IBOutlet firstChoiceButton?但是我所有的@IBOutlets

我正在制作一个互动故事应用程序。PageController:UIViewController控制故事,第一页加载良好,但当我进行选择时,应用程序崩溃,在展开时意外发现零致命错误。在线:

firstChoiceButton.setTitle(firstChoice.title, for: UIControlState())
查找它,似乎有一个可选的。。。也许它没有和我的Story.swift文件通信

也许是@IBOutlet firstChoiceButton?但是我所有的@IBOutlets/行为都是正确的,我想。。。帮忙

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

您应该以编程方式实例化视图控制器,并将其推送到导航控制器堆栈。下面是一些帮助您入门的代码

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

谢谢你的时间!